ClassyTcl object system
The ClassyTcl object system was created as an object system that
goes well with Tcl (IMHO), rather than being modelled
after the object system of another language. Some criteria were:
- Tcl is a dynamic language: so classes, methods, etc should also
be changeable at runtime
- no Tcl core changes
- fast, little overhead
- Tcl only possible, C code also possible for better performance
- Class methods can be defined in Tcl as wel as in C, and also in
a mix of the two
- Tk like working model
Classes and objects
In ClassyTcl classes and objects are very similar: they are both
entities that combine data and methods (actions that can be
performed). However, classes are mainly used as a template to
produce a number of objects or instances. The class of an object
determines wich data it stores, and which methods it has available.
New classes can be created by subclassing existing classes. The
subclass inherits the data and methods of its parent class. Extra
data and methods can be added after the new class is created.
Inherited methods can be replaced or removed.
Classmethods and methods
Class provides two types of methods:
- A classmethod is a command associated with a class. A new
classmethod can be defined using the classmethod classmethod
(which is defined in the basic class named Class
and always inherited). A classmethod can be invoked using the
className classmethod name arguments body
Classmethods are only available to their class, and cannot be
invoked from instances (objects) of that class.
- A method can be defined using the classmethod method.
A method of a class is available to all instances (objects) of the
class. A method can be invoked using a command of the form:
pathName method name arguments body
Methods and classmethods starting with an underscore are hidden: they
can be invoked, but are not shown when methods are queried. A class
can have both a method and a classmethod with the same name. If this
is the case, the method is invoked when doing:
object methodName ?...?
and the classmethod is used when doing:
class methodName ?...?
Each object (or class) can store its data in private variables. A
private variable should only be used by the object owning it. In
ClassyTcl, an object or a function can access the private variables
of another object, which is great for debugging. However, it is not
usually good object oriented programming practice to rely on this
feature for your programs (data encapsulation). Private variables can
be accessed using the following commands:
- private object var ?var? ...
- make the local variables in the list refer to the private
variables of $object
- setprivate object var value
- set the private variable $var of object $object to $value
- getprivate object var
- returns the current value of the private variable $var of object
- privatevar object var
- returns the fully specified name of the private variable $var
of object $object. This can eg. be used to link a private variable
to an entry:
entry .e -textvariable [privatevar someobject somevar]
Private variables can also be accessed via the method "private".
- object/className private
- returns a list of private variables of the class or object
- object/className private varName
- returns the current value of the private variable given by varName
- object/className private varName value
- sets the current value of the private variable given by varName to
the given value
Creation and Destruction of new classes
A new class can be created using the method subclass:
SomeClass subclass SubClass
The new subclass inherits all methods, classmethods and private variables of
its superclass. If the methods, classmethods of a class are changed, they
will changed for all its subclasses as wel. Changes of private classvariables
are also propagated, but only if the are changed using the private method.
A class is destroyed by invoking its destroy classmethod:
The destroy classmethod can be redefined. However, it cannot have arguments,
and will always succeed: errors in it will not be reported.
Creation and Destruction of new objects
A new object can be created using the classmethod new:
SomeClass new objectName
Upon creation of a new object, the init classmethod of the class of the object
being created will be invoked.
Usually, the init classmethod will contain the following line:
super init ?...?
If the init classmethod was redefined by the class, this code invokes the init
classmethod of its superclass. If the init classmethod was inherited
from the superclass, "super init" will find the first superclass of the class
that defines a different init classmethod, and invokes that. (
"super methodName ?...?" can also be used to invoke previous definitions of
other methods or classmethods.)
Objects are destroyed by invoking their destroy method:
the destroy method can be redefined. However, upon destruction, all
destroy methods defined in superclasses of the class will be invoked as wel.
A destroy method cannot have arguments, and must always succeed: errors in it
will not be reported.
When the package Class is loaded, it will create one base class
named Class. All other classes and
object will be derived from this base class. The
classmethods and methods defined by Class can normally be invoked
from all classes.