Objecttypes
HolonTForth implements a simple objecttype mechanism. Objects have private data and access methods that are activated by messages. There is no inheritance, objects are static with early binding. Example:
Variable
Define: 55 variable var
Use:
var 5 + . 5 var add var print var incr 77 var set
for Forth freaks also: var @ 55 * var !
Messages and methods are separate items. The messages are defined as
Message get
Message set
Message add
...
The corresponding methods are scripts stored in an array for each objecttype. Example:
Objecttype variable
set variable(instance) {set obj [pop]} set variable() {push $obj} ;# default method set variable(get) {push $obj} set variable(@) {push $obj} set variable(set) {set obj [pop]} set variable(!) {set obj [pop]} set variable(incr) {incr obj} set variable(add) {set obj [expr {$obj+[pop]}]} set variable(print) {puts -nonewline $obj}
The "instance" method is used when an object is created, it defines the instance data.
If the object is used without a message, the default message () is used.
Several messages call the same action, e.g. get and @, set and !. Choose what you prefer, or add your own!
HolonTForth presently also defines the following objecttypes.
String
Messages: get @ set ! print append index length tolower
Example: " a little text " string S Use: " for you" S append S print
Array
Messages: get @ set ! incr add names
Example: {} array A Use: 11 1 A set 22 2 A set 1 A 2 A + ( -- 33 )
List
Messages: index @ get set ! setlist getlist append search revert sort length print
also to use a list as a stack: push pop
Example: { 11 22 33} list L Use: 1 L index ( -- 22 ), 1 L get ( -- 22 ), 1 L ( -- 22 ) 1 L 2 L + ( -- 55 ) L getlist ( -- {11 22 33} ) 888 L push L getlist ( -- {11 22 33 888} ) L pop ( -- {11 22 33} 888 )
File
Messages: name get put open-w open-r close eof
Example: " source.tcl" file sfile \ file object Use: sfile open-r begin sfile get .cr \ read a line and print sfile eof
until sfile close