1copy(n) TclOO Commands copy(n)
2
3
4
5______________________________________________________________________________
6
8 oo::copy - create copies of objects and classes
9
11 package require TclOO
12
13 oo::copy sourceObject ?targetObject? ?targetNamespace?
14______________________________________________________________________________
15
17 The oo::copy command creates a copy of an object or class. It takes the
18 name of the object or class to be copied, sourceObject, and optionally
19 the name of the object or class to create, targetObject, which will be
20 resolved relative to the current namespace if not an absolute qualified
21 name and targetNamespace which is the name of the namespace that will │
22 hold the internal state of the object (my command, etc.); it must not │
23 refer to an existing namespace. If either targetObject or targetNames‐ │
24 pace is omitted or is given as the empty string, a new name is chosen. │
25 Names, unless specified, are chosen with the same algorithm used by the │
26 new method of oo::class. The copied object will be of the same class
27 as the source object, and will have all its per-object methods copied.
28 If it is a class, it will also have all the class methods in the class
29 copied, but it will not have any of its instances copied.
30
31 After the targetObject has been created and all definitions of its con‐ │
32 figuration (e.g., methods, filters, mixins) copied, the <cloned> method │
33 of targetObject will be invoked, to allow for customization of the cre‐ │
34 ated object such as installing related variable traces. The only argu‐ │
35 ment given will be sourceObject. The default implementation of this │
36 method (in oo::object) just copies the procedures and variables in the │
37 namespace of sourceObject to the namespace of targetObject. If this │
38 method call does not return a result that is successful (i.e., an error │
39 or other kind of exception) then the targetObject will be deleted and │
40 an error returned.
41
42 The result of the oo::copy command will be the fully-qualified name of
43 the new object or class.
44
46 This example creates an object, copies it, modifies the source object,
47 and then demonstrates that the copied object is indeed a copy.
48
49 oo::object create src
50 oo::objdefine src method msg {} {puts foo}
51 oo::copy src dst
52 oo::objdefine src method msg {} {puts bar}
53 src msg → prints "bar"
54 dst msg → prints "foo"
55
57 oo::class(n), oo::define(n), oo::object(n)
58
60 clone, copy, duplication, object
61
62
63
64TclOO 0.1 copy(n)