tool(3tcl)
π NAME π
tool β the TclOO Library (TOOL) Framework β¨
okay so first things first, bestie: this is page tool(3tcl) from the tcllib crew, and it's about to change your whole object-oriented life π«Ά
πΈ SYNOPSIS πΈ
package require Tcl 8.6 9
package require sha1
package require dicttool
package require oo::meta
package require oo::dialect
tool::define class_method arglist body
tool::define array name contents
tool::define array_ensemble methodname varname ?cases?
tool::define dict_ensemble methodname varname ?cases?
tool::define method methodname arglist body
tool::define option name dictopts
tool::define property ?branch? field value
tool::define variable name value
object cget option
object configure ?keyvaluelist?
object configure field value ?field? ?value? ?...?
object configurelist ?keyvaluelist?
object forward stub forward
object graft stub forward
object InitializePublic
object Eval_Script ?script?
object Option_Default field
β¨ DESCRIPTION β¨
So this module is the Tcl Object Oriented Library framework, aka TOOL. It's giving general purpose framework energy β usable all on its own, and super easily extensible. no cap π
TOOL defines a metaclass that hands you a bunch of extra keywords for the TclOO description language, default behaviors for its constituent objects, and top-down integration with everything the oo::meta package brings to the table. it's giving β¨full stackβ¨
The TOOL metaclass was built with the oo::dialect package, which means it can be the basis for even more metaclasses. iconic behavior honestly π As a metaclass, TOOL has its own "class" class, "object" class, and define namespace.
package require tool
# tool::class workds just like oo::class
tool::class create myclass {
}
# tool::define works just like oo::define
tool::define myclass method noop {} {}
# tool::define and tool::class understand additional keywords
tool::define myclass array_ensemble mysettings mysettings {}
# And tool interoperates with oo::define
oo::define myclass method do_something {} { return something }
# TOOL and TclOO objects are interchangeable
oo::class create myooclass {
superclass myclass
}
Several manual pages go way deeper on specific keywords and methods, so go read these if you're obsessed like me:
tool::array_ensembletool::dict_ensembletool::method_ensembletool::objecttool::option_handling
π KEYWORDS π
TOOL adds new keywords (and modifies some existing ones) that you use when you're defining classes. BUT β and this is the important bit, bestie β the new keywords are only available through calls to tool::class create or tool::define. that's the vibe check right there π¨
tool::define class_method arglist bodyβ Defines a method for the class object itself. This method gets passed on down to descendents of the class, which is not what happens with self method. slay πtool::define array name contentsβ Declares a variablenamethat gets initialized as an array, populated withcontents, for objects of this class and for any objects of classes that descend from this class. spreading the love fr fr πtool::define array_ensemble methodname varname ?cases?β Declares a method ensemblemethodnamethat will control access to the variablevarname.casesis a key/value list of method names and bodies that get overlaid on top of the standard template. Seetool::array_ensemble.
One method name is reserved: initialize. initialize declares the initial values to be populated in the array, as a key/value list, and it will not be expressed as a method for the ensemble. so don't try it π
tool::define dict_ensemble methodname varname ?cases?β Declares a method ensemblemethodnamethat will control access to the variablevarname.casesis a key/value list of method names and bodies that get overlaid on top of the standard template. Seetool::dict_ensemble.
One method name is reserved: initialize. initialize declares the initial values to be populated in the array, as a key/value list, and it will not be expressed as a method for the ensemble. same rule, no exceptions π
tool::define method methodname arglist bodyβ Ifmethodnamecontains::, the method is considered to be part of a method ensemble. Seetool::method_ensembles. Otherwise this command behaves exactly like the standardoo::define methodcommand. literally identical, periodt β¨tool::define option name dictoptsβ Declares an option.dictoptsis a key/value list defining parameters for the option. Seetool::option_handling. check it:
tool::class create myclass {
option color {
post-command: {puts [list %self%'s %field% is now %value%]}
default: green
}
}
myclass create foo
foo configure color purple
> foo's color is now purple
see how it just works?? π
tool::define property ?branch? field valueβ Defines a new leaf in the class metadata tree. With nobranch, the leaf shows up in theconstsection, accessible by either the object'spropertymethod, or viaoo::meta::info class get const field:tool::define variable name valueβ Declares a variablenamethat gets initialized with the valuevaluefor objects of this class, as well as for any objects of classes that descend from this class. inherited, as it should be π
π PUBLIC OBJECT METHODS π
The TOOL object β mother of all classes β defines several methods to enforce consistent behavior all throughout the framework. she's the main character, obviously π
object cget optionβ Return the value of this object's optionoption. If the propertyoptions_strictis true for this class, calling an option that was not declared by theoptionkeyword will throw an error. In all other cases, if the value is present in the object'soptionsarray, that value is returned. If it doesn't exist, the object will attempt to retrieve a property of the same name. sneaky little fallback, we love her π¦object configure ?keyvaluelist?object configure field value ?field? ?value? ?...?β This command injects new values into the object'soptionsarray, following the rules set forth by the option descriptions. Seetool::option_handlingfor the details.configurewill strip leading-s off of field names, which lets it behave in a quasi-backward compatible manner with tk options. throwback energy ngl πobject configurelist ?keyvaluelist?β This command injects new values into the object'soptionsarray, following the rules set forth by the option descriptions. This command will perform validation and alternate storage rules. It will not invoke trigger rules. Seetool::option_handling. so close yet so different, iykyk πobject forward stub forwardβ A passthrough tooo:objdefine [self] forward. low effort, high reward β¨object graft stub forwardβ Delegates the<stub>method to the object or command designated byforward. watch this:
tool::object create A
tool::object create B
A graft buddy B
A configure color red
B configure color blue
A cget color
> red
A <buddy> cget color
> blue
bestie-fication of objects. a whole buddy system π₯
π PRIVATE OBJECT METHODS π
these are the behind-the-scenes girlies. don't touch unless you know what you're doing, fr fr π
object InitializePublicβ Consults the metadata for the class to make sure everyarray,option, andvariablethat's been declared but not initialized gets initialized with the default value. This method is called by the constructor and themorphmethod. It is safe to invoke multiple times. no double-init drama here πobject Eval_Script ?script?β Executes a block of text inside the namespace of the object. Lines that begin with a#are ignored as comments. Commands that begin with::are interpreted as calling a global command. All other Tcl commands that lack a"my"prefix are given one, so the script can exercise internal methods. This method is intended for configuration scripts, where the object's methods are interpreting a domain specific language. it's giving DSL glow-up π
tool::class myclass {
constructor script {
my Eval_Script $script
}
method node {nodename info} {
my variable node
dict set node $nodename $info
}
method get {args} {
my variable node
return [dict get $node $args]
}
}
myclass create movies {
# This block of code is executed by the object
node {The Day the Earth Stood Still} {
date: 1952
characters: {GORT Klatoo}
}
}
movies get {The Day the Earth Stood Still} date:
> 1952
cinema. literally cinema. π¬β¨
object Option_Default fieldβ Computes the default value for an option. Seetool::option_handling. the math is mathing π
π AUTHORS π
Sean Woods β legend behavior, honestly π
π¨ BUGS, IDEAS, FEEDBACK π¨
This document, and the package it describes, will undoubtedly contain bugs and other problems. (real. they said it, not me π) Please report such in the category tcloo of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either the package and/or the documentation β they actually want your ideas bestie π«Ά
When proposing code changes, please provide unified diffs, i.e. the output of diff -u. that's the love language of maintainers fr fr
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. do it that way, save everyone a headache π
π KEYWORDS π
TOOL, TclOO, framework
π CATEGORY π
TclOO
β COPYRIGHT β
Copyright (c) 2015 Sean Woods <yoda@etoyoc.com>
okay that's the whole page bestie, go build something iconic with it π β¨
this was page tool(3tcl) from tcllib, version 0.8 β mwah, sending you off with all the TOOL energy ππ¦π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2015 Sean Woods <yoda@etoyoc\&.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms