Poet2.2 Manual Prototype Object Extension for Tcl


Poet uses dynamic, prototype-based inheritance.  That means that any object can act as a parent for any other object, and the ancestors of an object are searched anew each time an item is sought (instead of searching once when the object is created and storing that statically).  There is no such thing as a class, though it is common to have some objects that behave like a class and just have methods, with descendant objects that have only data (slots).

Objects are constructed by their parent using the construct method.  Poet defines the ultimate prototype object Object, which is the ancestor to all Poet objects.  All of an object's public slots (see Slots) and all of its methods, formulas, and types are available to all its descendants.  

An object can include additional parent objects using the method mixin. The mixin may itself be descendant from the object Mixin, which can have a methods that are automatically invoked when it is mixed in and when it is mixed out (removed as a parent).

When the inheritance tree is searched, the $self object is searched first.  If the item is not found, the parents are searched, in order (the parent first, then the mixins, in the order they were mixed in). The search then continues with the parents' ancestors. So if an object inherits a method from its parent and mixes in an object with a definition of the same method, the mixin will not override the parent's method.  However, if the method originates on the grandparent, and a mixin with the same method is mixed in, it will override the grandparent's method. Remove the mixin as a parent (with unparent) and the grandparent's method will again become the one found.

The objects intended as mixins in the Poet library use a naming convention of prefixing their slots and methods with the name of the mixin, so they're unique and the order in which the inheritance tree is searched isn't relevant.  Mixins that intentionally override existing methods can be a very powerful tool for switching between different sets of behaviors.