On Thursday 09 August 2001 02:42 pm, you wrote:

> Can someone explain under what circumstances one would want to change the 
> class of an object?  An example of the idiom would be useful.

Ruby allows a limited form of this already, since I can specify a 
per-instance version of a method. This actually (under the covers) creates a 
new class that is a copy of the old class in which the method is changed (as 
I understand).

A case where I'm changing classes of objects usefully in Perl is in handling 
Zip files. Each Zip file has a number of members. These members are of 
different kinds (some may have been read from the zip file, some may have 
data in new disk files, or in strings, etc.). Each kind of member has 
somewhat different versions of a number of methods, so it makes sense to have 
them be different classes.

However, it is possible to write to a member, thereby changing its data. But 
because members whose data come from strings in memory have different 
behavior than members whose data came from a zip file, I need to change the 
member's class. I could replace the member in the Zip, but other objects may 
be referring to the member already, and I'd invalidate their references if I 
did this. Since changing classes in Perl is much easier than changing object 
identity (see below), I just "re-bless" (change the class) of the member when 
it is written to (as well as changing some of its internal data, if 
necessary).

> so can you explain the purpose of swapping the identities of the objects?

Sure. One example would be in a system where you had proxy objects 
representing, say, objects that were out in disk files. These would be 
placekeepers (allowing existing objects to maintain their references) and 
could load on demand. However, when I re-load an object that I had a proxy 
to, I want all the other objects that pointed to the proxy to now point to 
the real object.

Being able to swap their identities allows me to do this.

This is a trivial operation on the old Smalltalks that used double 
indirection to get to objects; it amounted to swapping two values in the 
object table. It's considerably more costly when object references are 
implemented as direct memory pointers, as you may have to hunt down all the 
old pointers.

-- 
Ned Konz
currently: Stanwood, WA
email:     ned / bike-nomad.com
homepage:  http://bike-nomad.com