TIP 397: Extensible Object Copying

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
State:		Final
Type:		Project
Tcl-Version:	8.6
Vote:		Done
Post-History:	
Author:		Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Created:	13-Feb-2012
Keywords:	Tcl, TclOO, copy, clone

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: https://core.tcl.tk/tcloo/timeline?r=development-rfe3485060

Copyright

This document has been placed in the public domain.

History