copy(n)
π copy(n) β the TclOO clone-your-faves manual π
okay bestie, gather round, because we're about to talk about oo::copy, the TclOO command that lets you make a whole twin of an object or a class. it's giving duplication, it's giving main character energy, and yes it lives in the TclOO command family. let's go π¦
β¨ SYNOPSIS β¨
package require tcl::oo
oo::copy sourceObject ?targetObject? ?targetNamespace?
like. three whole arguments and two of them are optional. we love a low-maintenance icon π
πΈ DESCRIPTION πΈ
so the oo::copy command creates a copy of an object or class. periodt.
it takes the name of the object or class to be copied β that's your sourceObject, the original, the blueprint, the moment β and optionally:
- the name of the object or class to create,
targetObjectβ and heads up, if that name isn't an absolute qualified name, it gets resolved relative to the current namespace. iykyk. targetNamespace, which is the name of the namespace that will hold the internal state of the object (themycommand, etc.). β οΈ and it must not refer to an existing namespace. not negotiable, bestie.
if either targetObject or targetNamespace is omitted, or is given as the empty string, a new name is chosen. and how are those names chosen? with the same algorithm used by the new method of oo::class. slay, consistent naming behavior, we stan. π
so what actually gets copied, you ask? π
- the copied object will be of the same class as the source object, and will have all its per-object methods copied.
- if it's a class, it will also have all the class methods in the class copied β but it will not have any of its instances copied. nope. those stay home. π
πͺ the <cloned> method moment πͺ
after the targetObject has been created and all definitions of its configuration (e.g., methods, filters, mixins) have been copied, the <cloned> method of targetObject will be invoked β this is your customization era, it lets you do stuff like installing related variable traces. β¨
the only argument given will be sourceObject.
the default implementation of this method (in oo::object) just copies the procedures and variables in the namespace of sourceObject to the namespace of targetObject. simple, elegant, iconic.
π important though: if this method call does not return a result that is successful (i.e., an error or other kind of exception), then targetObject will be deleted and an error returned. no half-cloned weirdness, we clean up after ourselves.
and the result of the whole oo::copy command? it's the fully-qualified name of the new object or class. π
π₯ EXAMPLES π₯
this example creates an object, copies it, modifies the source object, and then demonstrates that the copied object is indeed a copy. literally the perfect little demonstration, obsessed.
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 went off and changed its whole vibe to bar, and dst is still out here printing foo? that's the copy doing its job. no cap. π«Ά
π SEE ALSO π
oo::class(n)oo::define(n)oo::object(n)
π·οΈ KEYWORDS π·οΈ
- clone
- copy
- duplication
- object
and that's the whole page, bestie π this was TclOO, version 0.1, bringing you the copy(n) manual β go forth and duplicate responsibly, mwah π
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- 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