Disclosure: I wrote TclOO (with a lot of help from others in development and testing).
Simple start
TclOO allows for very simple use, but can become extremely complex when you start using most of its functions. Here is an example:
# Make a class oo::class create Example { variable x ;# Not the same as [variable] in a namespace! constructor {} { set x 1 } method bar {} { return [incr x] } } Example create foo ;# Make an instance puts [foo bar] ;# Call the instance to get 2 puts [foo bar] ;# Call the instance to get 3 puts [foo bar] ;# Call the instance to get 4 foo destroy ;# Kill the instance
Writing a class is pretty simple, and the above gives you enough to do a lot. There are several main functions that are not listed: superclass allows you to name the parent class of the class, by default it is oo::object , which is the class of all objects; forward allows you to send a method call to another command, simple delegation; destructor allows you to write what is called when the object leaves; execution of Example new will make the object without its name, the name of the created object is the result of its call; the name of the current object is the result of calling self inside the method.
Constructors and methods can take arguments in the same way as the main Tcl proc . Destructors cannot.
More difficult
Objects can be rename d, like any other Tcl command, and a whole host of introspections are available for them under the info object and info class . You can attach special behavior for each object using oo::objdefine . Each object has a private namespace that you can use to store state in (where is the variable x in the above example).
Methods are not exported by default unless their name begins with a lowercase letter (strictly, it depends on whether it matches the ball pattern " [az]* "). You can change this if you want.
Classes themselves are objects (instances of oo::class ), so they are created by calling oo::class create ; their constructor passes the script that you provide to the oo::define command, which is responsible for determining the behavior of the classes. The create and new methods are as follows: methods on classes that create instances of these classes (with / without name, respectively).
You can use multiple inheritance. And mixins. And filters. And add a submit handler to deal with attempts to call an unknown method.
You can subclass oo::class yourself so that you can define new ways to create and manage objects.
You can change the class of any object at runtime (except oo::object and oo::class , they are specially locked for sanity reasons).
...
Yes, I am the author of TclOO, but I'm still learning what my creation can do. I tried very hard to make sure that you will do almost everything that you ask about it.