| Store | Cart

[TCLCORE] Tcl Objects with value semantics

From: Duquette, William H (393K) <will...@jpl.nasa.gov>
Mon, 27 Jul 2015 19:56:22 +0000
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

Recent Messages in this Thread
Duquette, William H (393K) Jul 27, 2015 07:56 pm
Peter S Jul 28, 2015 12:07 pm
Duquette, William H (393K) Jul 28, 2015 03:23 pm
Cyan Ogilvie Jul 28, 2015 12:28 pm
Donal K. Fellows Jul 28, 2015 02:07 pm
Duquette, William H (393K) Jul 28, 2015 08:15 pm
Messages in this thread