Version 3 of Smalltick

Updated 2004-05-21 07:57:53 by GPS

Smalltalk like object system in pure-Tcl.

Author: GPS

The code that was here was out of date. If you want the latest see: http://www.xmission.com/~georgeps/Smalltick/

Smalltick is not a typical Tcl object system. It uses instance inheritance, so you aren't limited by the definition of a class. You can add new methods dynamically easily. It does not use namespaces to store methods and variables, but it also doesn't prevent you from using them. Instance variables are stored in a private array, and each method uses a unique-command-name prefix, so collisions are not a problem.

Classes in Smalltick are just procs with commands that associate variables and methods with an object.

 $ tclsh8.4
 % source ./Smalltick.tcl
 % set obj [new.object]
 cmd-2108840903
 % $obj set {var value}
 value
 % $obj get var
 value
 % $obj : foo {} { puts FOO }
 cmd-2108840903->foo
 % $obj foo
 FOO
 % $obj : bar arg { $self set [list var $arg] }
 cmd-2108840903->bar
 % bar 123
 invalid command name "bar"
 % $obj bar 123
 123
 % $obj get var
 123

You may notice that the instance variable setting is a little weird. This is due to the generic application of method invocation. For instance $obj -foo arg -bar arg is generalized to treat the argument to the method as a single list. If you want to pass multiple arguments define a method like so: $obj : mul {a b} {expr {$a + $b}} and then use it in this manner: $obj mul list 1 2


Category Object Orientation