1Module: TOOL
2============
3
4TOOL is the Tcl Object Oriented Library, a standard object framework. TOOL
5implements common design patterns in a standardized, tested, and documented
6manner.
7
8# Major Concepts
9
10* Metadata Interitance
11* Variable and Array Initialization
12* Option handling
13* Delegation
14* Method Ensembles
15
16## Using TOOL
17
18Tool is accessed from the "tool" package:
19
20<pre><code>
21package require tool
22</code></pre>
23
24## Metadata Interitance
25
26TOOL builds on the oo::meta package to allow data and configuration to be
27passed along to descendents in the same way methods are.
28
29<pre><code>tool::class create fruit {
30  property taste sweet
31}
32tool::class create fruit.apple {
33  property color red
34}
35tool::class create fruit.orange {
36  property color orange
37}
38fruit.orange create cutie
39cutie property color
40> orange
41cutie property taste
42> sweet
43</code></pre>
44
45## Variable and Array Initialization
46
47TOOL modifies the *variable* keyword and adds and *array* keyword. Using
48either will cause a variable of the given name to be initialized with the
49given value for this class AND any descendents.
50
51<pre><code>tool::class create car {
52  option color {
53    default: white
54  }
55  variable location home
56  array physics {
57    speed 0
58    accel 0
59    position {0 0}
60  }
61
62  method physics {field args} {
63    my variable physics
64    if {[llength $args]} {
65      set physics($field) $args
66    }
67    return $physics($field)
68  }
69  method location {} {
70    my variable location
71    return $location
72  }
73  method move newloc {
74    my variable location
75    set location $newloc
76  }
77}
78
79car create car1 color green
80car1 cget color
81> green
82car create car2
83car2 cget color
84> white
85
86car1 location
87> home
88car1 move work
89car1 location
90> work
91car1 physics speed
92> 0
93car1 physics speed 10
94car1 physics speed
95> 10
96</code></pre>
97
98## Delegation
99
100TOOL is built around objects delegating functions to other objects. To
101keep track of which object is handling what function, TOOL provides
102two methods *graft* and *organ*.
103
104<pre><code>tool::class create human {}
105
106human create bob name Robert
107car1 graft driver bob
108bob graft car car1
109bob &lt;car&gt; physics speed
110> 10
111car1 &lt;driver&gt; cget name
112> Robert
113car1 organ driver
114> bob
115bob organ car
116> car1
117</code></pre>
118
119## Method Ensembles
120
121TOOL also introduces the concept of a method ensemble. To declare an ensemble
122use a :: delimter in the name of the method.
123
124<pre><code>tool::class create special {
125
126  method foo::bar {} {
127    return bar
128  }
129  method foo::baz {} {
130    return baz
131  }
132  method foo::bat {} {
133    return bat
134  }
135}
136
137special create blah
138bah foo <list>
139> bar bat baz
140bah foo bar
141> bar
142bar foo bing
143> ERROR: Invalid command "bing", Valid: bar, bat, baz
144</code></pre>
145
146Keep in mind that everything is changeable on demand in TOOL,
147and if you define a *default* method that will override the standard
148unknown reply:
149
150<pre><code>tool::define special {
151  method foo::default args {
152    return [list $method $args]
153  }
154}
155bar foo bing
156> bing
157</code></pre>
158