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 (the my command, 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