copy(3tcl)
π NAME π
oo::copy β bestie, this is the one that makes literal copies of objects and classes β¨
π¦ SYNOPSIS π¦
package require tcl::oo
oo::copy sourceObject ?targetObject? ?targetNamespace?
π DESCRIPTION π
Okay so, the oo::copy command makes a copy of an object or a class. Iconic behaviour, honestly. Here's the tea on how it works:
You hand it the name of the object or class you wanna copy, that's sourceObject, and then optionally the name of the object or class you want it to make, which is targetObject. If targetObject isn't an absolute qualified name, it gets resolved relative to the current namespace, no cap. Then there's targetNamespace, which is the name of the namespace that'll hold the internal state of the object (your my command, etc. live there) β and it must not refer to an existing namespace, so don't even try it π
If either targetObject or targetNamespace is omitted, or you pass it as the empty string, a new name gets chosen for you. And when names are chosen, they use the exact same algorithm as the new method of oo::class. Periodt.
So what do you actually get? The copied object will be of the same class as the source object, and it'll have all its per-object methods copied over. And if you're copying a class? It also gets all the class methods in the class copied too β but it will not have any of its instances copied. Not a single one. Those stay with the original, sorry bestie π
πΈ the <cloned> moment πΈ
So after targetObject has been created and all the definitions of its configuration (like methods, filters, mixins) have been copied, the <cloned> method of targetObject gets invoked. This is the glow-up moment β it lets you customize the freshly created object, like installing related variable traces, y'know, cute touches π
The only argument given to it will be sourceObject.
The default implementation of this method (it lives in oo::object) just copies the procedures and variables in the namespace of sourceObject over to the namespace of targetObject.
β οΈ But listen: if this method call does not return a successful result β meaning it errors or throws some other kind of exception β then targetObject gets deleted and an error is returned. So it's giving all-or-nothing energy π
β the result β
The result of the oo::copy command will be the fully-qualified name of the new object or class. Slay.
π₯ EXAMPLES π₯
This example creates an object, copies it, modifies the source object, and then demonstrates that the copied object is indeed a copy. Chef's kiss demo, fr fr.
oo::object create src
oo::objdefine src method msg {} {puts foo}
oo::copy src dst
oo::objdefine src method msg {} {puts bar}
src msg β prints "bar"
dst msg β prints "foo"
See how src changed to bar but dst is still out here printing foo? That's the copy doing its thing π«Ά
π SEE ALSO π
oo::class(3tcl)oo::define(3tcl)oo::object(3tcl)
π KEYWORDS π
clonecopyduplicationobject
And that's the whole vibe, bestie β oo::copy from TclOO, version 0.1. Go forth and duplicate responsibly π§β¨
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2007 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms