Skip to content

Objecttypes

ww edited this page Dec 3, 2014 · 31 revisions

#Objecttypes#

Tcl Variables and Commands#

Tcl has a rich set of data types. All variations of numbers and strings, lists, arrays, and dicts. The data are handled as arguments to a command. The data type is specific to the command but the data elements are all variables. The command checks if the contents of the variable conforms to the type.

There are two classes of commands. E.g. string commands are subcommands to a general command string, list commands are general.

  • string length string
  • llength list

###TclForth Objecttypes# TclForth provides a uniform way to handle the Tcl data types: As objects that respond to private methods. TclForth objecttypes are simple static classes without inheritance, that create instances and apply a method command.

  • string length
  • list length

The objecttypes are implemented as an associative array, wherein the names are the commands (messages) and the values are execution scripts (methods).

Example: Variable#

objecttype variable
     instance  {set obj [pop]}
     {}        {push $obj}
     get       {push $obj}
     @         {push $obj}
     set       {set obj [pop]}
     !         {set obj [pop]}
     ....

###Comments#

  • TclForth data types: Constant, Variable, Array, List, String, File
  • See the actual definitions in the source file forth.fth
  • The instance method is used when an object is created, it defines the instance data.
  • If an object is used without a message, the default message {} applies.
  • You can apply several messages to the same method: Being a Forth programmer you might prefer to use @ and !.

###Objecttype File# File is an example of a custom data type.

Objecttype file  
	instance  {set obj "[pop] handle" ; }
	{}        {@list $obj 1}   
	open-w    {push [open [lindex $obj 0] w]; !list obj 1 }
	open      {push [open [lindex $obj 0] r]; !list obj 1 }
	close     {close [lindex $obj 1]}
	put       {puts [lindex $obj 1] [pop]}
	get       {push [gets [lindex $obj 1]]}
	read      {push [read [lindex $obj 1]]}
	eof       {push [eof [lindex $obj 1]]}
Example:  " source.tcl" file sfile

Use:      sfile open-r
          begin sfile get .cr
                sfile eof
          until
          sfile close
Clone this wiki locally