Version 4 of On things

Updated 2003-08-14 15:01:57

Richard Suchenwirth - Everything is a thing. This tautology rhymes with our old Tcl mantra, but isn't always true. Here I show an OO API in the Tcl spirit of utmost simplicity, inspired by Self (see below). For implementation see Doing things in namespaces - Doing things. Itcl's famous toaster example is thingified in Toasters and things.

Let the command thing create things (which are something like objects, or classes, or frames...) by giving them a name and possibly other information (which gives you multiple one-level inheritance):

 thing human legs 2
 thing Socrates is-a {human philosopher}

Note that philosopher is a not-yet defined name here, it's just a string, and strings are implicitly also things. Thus, if everything is a string and a string is a thing, then everything is a thing. QED. (But seriously, strings can for instance not necessarily be used as command names, so let's better distinguish little things like strings and real things, of which this page deals). A little thing can always be raised to a real thing - by calling it "thing". So much for forward references...

Things have properties (variables, facets, ...) that can be set and got in the familiar way:

 human set mortal 1 ;# Humans are mortal
 Socrates set hair white
 Socrates set hair ;# returns ==>: white

According to the is-a list (a property things always have, and be it the empty list), properties are searched in "superthings" if not found in the thing itself:

 Socrates set mortal ;# ==> 1

Properties can be introspected (more orthogonally than set/info vars):

 Socrates set ;# ==> is-a mortal legs

Things can have ways (methods, demons, ...) that can be set and got in an even more orthogonal way (pure Lambda) than proc/info args/info body provide, and inherited like properties:

 human wayto sing {{text} {puts "$text, lala."}}
 Socrates wayto sing ;# ==> {{text} {puts "$text, lala."}}
 thing Plato
 Plato wayto sing [Socrates wayto sing] ;# knows how to sing, but isn't human
 Socrates wayto ;# ==> sing

and called like objects like to be called:

 Socrates sing "Lali" ;# ==> Lali, lala.

Special ways could be specified that get fired when a property is changed (write traces):

 Socrates way-if-set legs {{} {
         puts "Hey, I now have [self get legs] legs!"
 }

And good old introspection is always there:

 Socrates help ;#(error) help? use one of: set, unset, is-a, sing, ...
 thing -names ;#==> human Socrates Plato

That's what I have so far. Thoughts?


Andreas Kupries Not so much as thoughts, but a couple of associations. IOW, the above reminds me of

  • "prototype"-based OO systems [L1 ], for example "SELF" [L2 ] [L3 ]
  • Artificial intelligence (Artificial Intelligence with Tcl) and semantic nets, also case based reasoning. I once read the following book which had a chapter about this [L4 ] (Actually it was a german translation).

RS: Yes, I didn't credit it, but a few lines from Larry Smith about Self brought me to make up the above. Thanks for the links - just my printer is momentarily stuck...

MSW: It reminds me of MIT OTcl (which btw is the soundest OO system for Tcl I've ever seen :)


Category Object Orientation | Arts and crafts of Tcl-Tk programming