TclOO Package

Artifact Content
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Aug 20.

Artifact 9760bede06607e7bcc83dd18f738136143ac8205:

Wiki page [Doc: oo::copy] by dkf 2016-09-17 11:48:49.
D 2016-09-17T11:48:49.874
L Doc:\soo::copy
N text/x-markdown
U dkf
W 2534
<h1>oo::copy<h1>
<H3><A NAME="M2">NAME</A></H3>
<b>oo::copy</b> &mdash; create copies of objects and classes
<H3><A NAME="M3">SYNOPSIS</A></H3>
package require TclOO<BR>
<BR>
<B>oo::copy</B><I> sourceObject </I>?<I>targetObject</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>oo::copy</B> command creates a copy of an object or class. It takes the
name of the object or class to be copied, <I>sourceObject</I>, and optionally
the name of the object or class to create, <I>targetObject</I>, which will be
resolved relative to the current namespace if not an absolute qualified name.
If <I>targetObject</I> is omitted, a new name is chosen. The copied object will
be of the same class as the source object, and will have all its per-object
methods copied. If it is a class, it will also have all the class methods in
the class copied, but it will not have any of its instances copied.
<P>
After the <I>targetObject</I> has been created and all definitions of its
configuration (e.g., methods, filters, mixins) copied, the <B>&lt;cloned&gt;</B>
method of <I>targetObject</I> will be invoked, to allow for customization of
the created object such as installing related variable traces. The only
argument given will be <I>sourceObject</I>. The default implementation of this
method (in <B><A HREF="?name=Doc:+oo::object">oo::object</A></B>) just copies the procedures and variables in the
namespace of <I>sourceObject</I> to the namespace of <I>targetObject</I>. If
this method call does not return a result that is successful (i.e., an error
or other kind of exception) then the <I>targetObject</I> will be deleted and an
error returned.
<P>
The result of the <B>oo::copy</B> command will be the fully-qualified name of
the new object or class.
<H3><A NAME="M5">EXAMPLES</A></H3>
This example creates an object, copies it, modifies the source object, and
then demonstrates that the copied object is indeed a copy.
<P>
<PRE>oo::object create src
oo::objdefine src method msg {} {puts foo}
<B>oo::copy</B> src dst
oo::objdefine src method msg {} {puts bar}
src msg              <I><font size="+1">&#8594;</font> prints &quot;bar&quot;</I>
dst msg              <I><font size="+1">&#8594;</font> prints &quot;foo&quot;</I></PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="?name=Doc:+oo::class">oo::class</A></B>, <B><A HREF="?name=Doc:+oo::define">oo::define</A></B>, <B><A HREF="?name=Doc:+oo::object">oo::object</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
clone, copy, duplication, object
Z a53d8a7c848cc2304b4c1b8785f02a70