-
Notifications
You must be signed in to change notification settings - Fork 9
Objecttypes
Tcl has a rich set of data types. There are all common variations of numbers. And strings, lists, arrays, and dicts (dictionaries). The Tcl 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 of the command.
There are two classes of commands. E.g.:
- string length string
- llength list
String commands are subcommands to a general command string, list commands are general.
TclForth provides a uniform way to handle the Tcl data types as objects that respond to private methods. TclForth objecttypes are static classes that create instances and apply methods.
-
string length
- list length
The objecttypes are implemented as associative arrays. The names are the commands (messages) and the values are execution scripts (methods) of an object.
objecttype variable
instance {set obj [pop]}
{} {push $obj}
get {push $obj}
@ {push $obj}
set {set obj [pop]}
! {set obj [pop]}
....
- 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 @ for get and ! for set.
- TclForth implements the data types: Constant, Variable, Array, List, String, File
- The objecttypes are defined in forth.fth. They can easily be expanded.
With a little knowledge of (the native language) Tcl create your own types, say, a file 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]]}
Note how it is used in data types.