A (mostly) better alternative to namespace ensemble:
==== oo::object create stash oo::objdefine stash {
variable Stash method set {name val} { set Stash($name) $val } method get {name} { return $Stash($name) }
} ====
A few things are noteworthy:
* we didn't define a [class]! [TclOO] lets us define [methods] directly on any object. * methods don't get in the way of command resolution inside the object: we ca
Historically, namespace ensembles have been plagued with one Tcl Warts: command resolution starts in the ensemble's namespace. Thus, subcommands with names like set are problematic:
==== namespace eval stash {
proc set {name val} { variable Stash set Stash($name) $val } proc get {name} { variable Stash return $Stash($name) } namespace export * namespace ensemble create
} ==== This example will fail, because stash set accidentaly calls itself! This can be worked around by using ::set, but such changes are ugly. And not just cosmetically: try extending an existing ensemble with a set method, or gracefully using namespace path.
TclOO provides a neat solution to this: simply use an object.
Nested ensemble objects (hint: oo::objdefine forward). More rationale? Link some of the wart discussions here for a solution (and clean them up).
* [TclOO Tricks], where this concept appears as a "Singleton". * [Inspecting TclOO] is handy to see exactly what's defined on your object. * [Extending TclOO with metaclasses] might be relevant. * [namespace] and [namespace ensemble] talk about some of these issues. * [Object method ensemble] is a different thing, but may be useful in conjunction.