Howdy!
One of the things I've wanted in Tcl for a long time are objects with value semantics: something like dicts, but with callable methods that can easily update the object's fields. The point is not to have to manage the object life-span: the object exists so long as it's in a variable, and if the variable goes out of scope the object goes with it.
I came up with an approach yesterday that makes use of the "apply" command's anonymous function syntax to define dictionaries with methods. The scheme uses a helper command called "@"; you can write code like this:
set a [dict create]
@ a.first = "Fred"
@ a.last = "Flintstone"
@ a.count = 0
set b [@ a.last]
@ a.greet method {name} {
puts "[@ this.first] [@ this.last] says hello, $name"
@ a.count := {[@ a.count] + 1}
}
@ a.greet: "Barney"
It's a scheme that lends itself to prototype-based object definition.
It occurs to me that this might be a first step toward a better dictionary syntax. In a string or argument, "@dict.field" returns the value of the field; as the first token of a command, "@dict.field" gives access to a variety of things depending on the subcommand, as suggested above.
I've sketched out the beginning of the implementation for the basic scheme; it's at https://github.com/wduquette/atdict.
Will
--
Will Duquette -- Will...@jpl.nasa.gov
Athena Development Lead -- Jet Propulsion Laboratory
"It's amazing what you can do with the right tools."
------------------------------------------------------------------------------
_______________________________________________
Tcl-Core mailing list
Tcl-...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tcl-core