After some time writing Tcl/Tk code, I felt I needed a way to better organize my
code, and why not use an object oriented approach. As we use Tcl quite
extensively in our software products, we decided to use both the strict Tcl
implementation with no extension and a modified wish compiler with embedded
crypting to deliver Tcl applications in a binary form to our customers.

The reason we do not use extensions is to better track and follow the Tcl/Tk
versions easily (this may or may not be right, but this is not the point).

This object extension is based on 2 functions, new and delete that handle all
object members allocation and deallocation, as well as calling the constructor
and destructor functions. Object members (data) are held in a global array
bearing the class name and indexed with a unique integer id that new{} keeps
track of.

A simple parallel with C++ will make it easier to understand:

C++:

    className::className(someType p1, someType p2)
    {
        someObject.someMember = someThing;
    }
    className::~className(void)
    {
        ...
    }

    className someObject = new className(parameter1, parameter2);
    a = someObject->doSomething(parameter);
    b = someObject->someMember;
    delete someObject;

Tcl:

    proc className::className {id p1 p2} {
        # new{} keeps track of object ids and passes a unique one to the
        # constructor
        global className
        set className($id,someMember) someThing
    }
    proc className::~className {id} {
        # delete calls this procedure then take care of deallocating className
        # array members for this id
        ...
    }

    someObject = [new className $parameter1 $parameter2]
    # calls className::className{}
    set a [className::doSomething $someObject $parameter]
    set b $className($someObject,someMember)    
    delete className $someObject;
    # calls className::~className{} then free members data


Yes, I know, the syntax is not very pretty, but it does the job nicely and
simply once you are used to it. In any case, you don't have to use it much in
order to utilize this little pie implementation.


