TIP #397: EXTENSIBLE OBJECT COPYING ===================================== Version: $Revision: 1.4 $ Author: Donal K. Fellows State: Final Type: Project Tcl-Version: 8.6 Vote: Done Created: Monday, 13 February 2012 URL: https://tip.tcl-lang.org397.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== This TIP proposes a mechanism whereby an object or class can provide additional control over how it gets copied to a new one by the *oo::copy* command. RATIONALE =========== I always knew that TclOO's *oo::copy* command was not particularly complete, in that only the C-level state was copied. The vast majority of state associated with an object (notably including its variables) was not copied on the grounds that the caller of *oo::copy* could do that for themselves. However, this has not proved particularly workable in practice once someone actually started to use the mechanism. It turns out that the callers of *oo::copy* expect a full copy of the object to be created. This is quite challenging because some things associated with an object are tricky to copy correctly. For example, if an object has a number of Tk widgets under its control, or has timer callbacks set up, these are very challenging for a generic object copying scheme to detect (and generic copying of Tcl commands is difficult anyway). Fortunately, we already have a mechanism for handling such per-object variation: methods. PROPOSED CHANGE ================= I propose that the *oo::copy* command will internally call the newly created object's ** method (passing in the name of the source object as the only argument) to allow for the customization of the copy. If the method fails (throws an exception), the copy will be destroyed and the error propagated. /copiedObject/ ** /sourceObject/ Furthermore, a default implementation of the ** method will be created in *oo::object* which will duplicate the variables and procedures (note, not other commands) of the source to the copy. This method will not be exported by default (nor should it be; the method is not intended to be called directly). REFERENCE IMPLEMENTATION ========================== See the "development-rfe3485060" branch of the tcloo repository: COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows