struct

Documentation can be found for various (tcllib) data structures.Provided structures, so far:


How is the tcllib struct::list going to work - I presume that it can't be namespace import'd to replace the core's list command? Is there anything that can be done to prevent this from accidentally happening?

DGP Did you try it? [namespace import] throws an error on any attempt to import a command that already exists in the target namespace. Why would you want to muck with the global namespace anyway? Just import [struct::list] into your own namespace, if you want to make use of it by the simple name list rather than the fully qualified ::struct::list.

You are writing code in your own namespace, right?


Often what people want from "struct" is a C-like datatype. This is much different from the tcllib data structures described above. Various people have coded Tcl extensions for this kind of "struct"; Iain B. Findleton's Containers appears to be a very nice data structure extension that includes one.

Many people have written small procs that implement a C like structure facility in Tcl. Check google nearly monthly for discussions. Here's a thread [L1 ] that covers one nice implementation.

The first exchange in that thread is also here at C Struct is Tcl!

And of course, now tcllib has record - which provides that type of functionality!


RS 2005-08-22: Here's a simple way of implementing structs with a descriptive list, and an interp alias:

#-- Define a "structure type":
 % set person {first last dept office cell home}
 first last dept office cell home
#-- Make an struct element accessor:
 % interp alias {} person@  {} lsearch $person
 person@
#-- A sample instance:
 % set example {John Smith R+D 1234 0123-45678 616-7189123}
 John Smith R+D 1234 0123-45678 616-7189123
#-- Retrieving an element by name:
 % lindex $example [person@ dept]
 R+D
#-- Replacing an element by name:
 % lset example [person@ dept] Mktg
 John Smith Mktg 1234 0123-45678 616-7189123

JJS Changing the alias name to "person's" makes the code read so much more naturally, at least for me:

 lset example [person's dept] Mktg

Maybe it only works for native English speakers?