File: tool.md

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (157 lines) | stat: -rw-r--r-- 2,979 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
Module: TOOL
============

TOOL is the Tcl Object Oriented Library, a standard object framework. TOOL
implements common design patterns in a standardized, tested, and documented
manner. 

# Major Concepts

* Metadata Interitance
* Variable and Array Initialization
* Option handling
* Delegation
* Method Ensembles

## Using TOOL

Tool is accessed from the "tool" package:

<pre><code>
package require tool
</code></pre>

## Metadata Interitance

TOOL builds on the oo::meta package to allow data and configuration to be
passed along to descendents in the same way methods are.

<pre><code>tool::class create fruit {
  property taste sweet
}
tool::class create fruit.apple {
  property color red
}
tool::class create fruit.orange {
  property color orange
}
fruit.orange create cutie
cutie property color
> orange
cutie property taste
> sweet
</code></pre>

## Variable and Array Initialization

TOOL modifies the *variable* keyword and adds and *array* keyword. Using
either will cause a variable of the given name to be initialized with the
given value for this class AND any descendents.

<pre><code>tool::class create car {
  option color {
    default: white
  }
  variable location home
  array physics {
    speed 0
    accel 0
    position {0 0}
  }

  method physics {field args} {
    my variable physics
    if {[llength $args]} {
      set physics($field) $args
    }
    return $physics($field)
  }
  method location {} {
    my variable location
    return $location
  }
  method move newloc {
    my variable location
    set location $newloc
  }
}

car create car1 color green
car1 cget color
> green
car create car2
car2 cget color
> white

car1 location
> home
car1 move work
car1 location
> work
car1 physics speed
> 0
car1 physics speed 10
car1 physics speed
> 10
</code></pre>

## Delegation

TOOL is built around objects delegating functions to other objects. To
keep track of which object is handling what function, TOOL provides
two methods *graft* and *organ*.

<pre><code>tool::class create human {}

human create bob name Robert
car1 graft driver bob
bob graft car car1
bob &lt;car&gt; physics speed
> 10
car1 &lt;driver&gt; cget name
> Robert
car1 organ driver
> bob
bob organ car
> car1
</code></pre>

## Method Ensembles

TOOL also introduces the concept of a method ensemble. To declare an ensemble
use a :: delimter in the name of the method.

<pre><code>tool::class create special {

  method foo::bar {} {
    return bar
  }
  method foo::baz {} {
    return baz
  }
  method foo::bat {} {
    return bat
  }
}

special create blah
bah foo <list>
> bar bat baz
bah foo bar
> bar
bar foo bing
> ERROR: Invalid command "bing", Valid: bar, bat, baz
</code></pre>

Keep in mind that everything is changeable on demand in TOOL,
and if you define a *default* method that will override the standard
unknown reply:

<pre><code>tool::define special {
  method foo::default args {
    return [list $method $args]  
  }
}
bar foo bing
> bing
</code></pre>