[sclug] TCL - every 'thing' is a string and its readonly!

Graham Swallow lists at information-cascade.co.uk
Thu Dec 15 13:53:30 UTC 2011


Following on from yesterdays SCLUUG:

In the original Sunscript TCL V6, everything was stored as a string,
floats, ints, lists, everything! Then in 1997-ish V8-ish,
the new object model defined an object as a header object,
a string of nbytes (possibly including nulls!), type_ptr and ref_count,
and an internal object payload union, user-defined but eg two void *
pointers.

Objects of fixed header size can morph type, and remain at the same address,
just the connected body changes, so (eg) a bit of plain text can be
compiled into
a tokenised bytecode thing, and still be referred to by the same pointers.

Unfortunately, this brings the requirement for everything to be read-only.
The official morphing, is only for when viewed as a string, or as a list,
or float, ...
the object value is not _supposed_ to change (so for your code - break the
rules!).

The documentation omits to mention that objects are readonly,
(variables are not objects), and the code does show objects being
overwritten,
but only when ref_count allows in-situ-recycling (eg list append).

Other 'things' which vary whilst remaining readonly, are keys or selectors,
like 'SOCK11' is a fixed key that selects a modifyable fd (but that fd is
not a tcl object).

A good compromise, is to create an object, whose string value is a name,
supposedly held in a dictionary, pointing to the object. Then store a link
to the object in the name-string-object, for really quick lookups.

Unfortunately, this makes ref_count'ing a bit risky,
in particular, Tk bind takes the objects string value, not the objects
address,
so you have to go back to 'knowing' when to delete objects,
rather than letting the last ref_count do it for you,
unless you are very, very careful.

ALSO: {string} is like single-quoted 'string' and the stored data is string
(no braces - parsed out from parent context).
ALSO: namespaces are good for site::type::methods and data store
ALSO: there is good support for autoload, searched dirs (maybe symb
linked), for code
ALSO: safe interpreters allow use of untrusted code - like javascript does
in browsers
ALSO: the lack of STRUCT is a real pain, write a C module, or scripted GET
objname fieldname ; DEL objname ; etc
ALSO: the bolt-on extra OOP modules are a tad naff, even the officially
adopted one(s)
ALSO: (L I S P) ;# becomes T C L <EOLN> ;# ie fewer {curlies} looks simpler
ALSO: having created an argv-word-separated-by-spaces language, the boolean
evaluator does it differently !!

Graham



More information about the Sclug mailing list