Version 1 of An objectifying hook for Tcl 9

Updated 2002-10-30 12:25:23

Below is a very rough idea... it might be doable in Tcl 8 (i.e. compatibly), I'm not sure... jcw


Suppose a Tcl value could be tagged by one extra bit: the tag would indicate that the value wants to behave as an object. So for example, the command:

    set v [blah ...]

would be turned into:

    [blah ...] set v

If the result of blah was tagged in this way. If not, then the result would simply be stored in v as usual.

That requires changes to "set" (which is the most important one for me at this stage), but perhaps also to "lindex" and others. And given that set is such a core function, the tag-check must be super fast. Which means either a bit in Tcl_Obj, or a change to the Tcl_ObjType vector.

The point of all this...

I am building a system which lets me write complex "expressions" of Tcl commands. Each returns an object (in the usual way: the name of a freshly created Tcl command), but the most important issue is cleanup. So far, I can get everything to clean up just dandy, by avoiding set. So instead of writing:

    set a [b ... [c ... [d ...] ... [e ...] ...] ... [f ...] ...]

I have to write:

    [b ... [c ... [d ...] ... [e ...] ...] ... [f ...] ...] as a

Everything works. It's in fact surprisingly simple to achieve full cleanup (through a bit of trace trickery), across proc invocations and all. But the one nastiness here, is that "set a ..." is trouble.

So if all the above commands were to return a value which instructs "set" to turn around and let the command object handle the setup, then "set a ..." would work ok. So would "set a(ha) ...".

The problem now moves to lset and lreplace and lappend not working, in that they would not clean up as the rest does. But this is definitely a second-order issue. The difference is that these special objects are aggregates, and will therefore be forbidden for storage.

To keep things in perspective all of this is only about adding machinery so automatic cleanup of (command) objects can be implemented (at the Tcl script level).

To put it anoteh rway... the above adds the ability to implement automatic object lifetimes (a form garbage collection).