1class(n) TclOO Commands class(n)
2
3
4
5______________________________________________________________________________
6
8 oo::class - class of all classes
9
11 package require TclOO
12
13 oo::class method ?arg ...?
14
16 oo::object
17 → oo::class
18______________________________________________________________________________
19
21 Classes are objects that can manufacture other objects according to a
22 pattern stored in the factory object (the class). An instance of the
23 class is created by calling one of the class's factory methods, typi‐
24 cally either create if an explicit name is being given, or new if an
25 arbitrary unique name is to be automatically selected.
26
27 The oo::class class is the class of all classes; every class is an
28 instance of this class, which is consequently an instance of itself.
29 This class is a subclass of oo::object, so every class is also an
30 object. Additional metaclasses (i.e., classes of classes) can be
31 defined if necessary by subclassing oo::class. Note that the oo::class
32 object hides the new method on itself, so new classes should always be
33 made using the create method.
34
35 CONSTRUCTOR
36 The constructor of the oo::class class takes an optional argument
37 which, if present, is sent to the oo::define command (along with the
38 name of the newly-created class) to allow the class to be conveniently
39 configured at creation time.
40
41 DESTRUCTOR
42 The oo::class class does not define an explicit destructor. However,
43 when a class is destroyed, all its subclasses and instances are also
44 destroyed, along with all objects that it has been mixed into.
45
46 EXPORTED METHODS
47 cls create name ?arg ...?
48 This creates a new instance of the class cls called name (which
49 is resolved within the calling context's namespace if not fully
50 qualified), passing the arguments, arg ..., to the constructor,
51 and (if that returns a successful result) returning the fully
52 qualified name of the created object (the result of the con‐
53 structor is ignored). If the constructor fails (i.e. returns a
54 non-OK result) then the object is destroyed and the error mes‐
55 sage is the result of this method call.
56
57 cls new ?arg ...?
58 This creates a new instance of the class cls with a new unique
59 name, passing the arguments, arg ..., to the constructor, and
60 (if that returns a successful result) returning the fully quali‐
61 fied name of the created object (the result of the constructor
62 is ignored). If the constructor fails (i.e., returns a non-OK
63 result) then the object is destroyed and the error message is
64 the result of this method call.
65
66 Note that this method is not exported by the oo::class object
67 itself, so classes should not be created using this method.
68
69 NON-EXPORTED METHODS
70 The oo::class class supports the following non-exported methods:
71
72 cls createWithNamespace name nsName ?arg ...?
73 This creates a new instance of the class cls called name (which
74 is resolved within the calling context's namespace if not fully
75 qualified), passing the arguments, arg ..., to the constructor,
76 and (if that returns a successful result) returning the fully
77 qualified name of the created object (the result of the con‐
78 structor is ignored). The name of the instance's internal names‐
79 pace will be nsName unless that namespace already exists (when
80 an arbitrary name will be chosen instead). If the constructor
81 fails (i.e., returns a non-OK result) then the object is
82 destroyed and the error message is the result of this method
83 call.
84
86 This example defines a simple class hierarchy and creates a new
87 instance of it. It then invokes a method of the object before destroy‐
88 ing the hierarchy and showing that the destruction is transitive.
89
90 oo::class create fruit {
91 method eat {} {
92 puts "yummy!"
93 }
94 }
95 oo::class create banana {
96 superclass fruit
97 constructor {} {
98 my variable peeled
99 set peeled 0
100 }
101 method peel {} {
102 my variable peeled
103 set peeled 1
104 puts "skin now off"
105 }
106 method edible? {} {
107 my variable peeled
108 return $peeled
109 }
110 method eat {} {
111 if {![my edible?]} {
112 my peel
113 }
114 next
115 }
116 }
117 set b [banana new]
118 $b eat → prints "skin now off" and "yummy!"
119 fruit destroy
120 $b eat → error "unknown command"
121
123 oo::define(n), oo::object(n)
124
126 class, metaclass, object
127
128
129
130TclOO 0.1 class(n)