Changing the Class of an Object
spair at advantive.com
Fri Dec 15 17:09:14 UTC 2000
> On Thu, 14 Dec 2000, Andrew P. Black wrote:
> > It would still be nice if SP's changset made it as a plugin or
> > something, for benefit of those of us who have not yet learned to
> > enjoy building VMs.
> Well, the #changeClassToThatOf: method does essentially the same, but it
> can easily ensure that the class layout is the same, so it's much safer to
> use (and so could eventually make it into the base image). And even then,
> the weird selector is much more appropriate than SP's innocious looking
> but highly dangerous selector #class:.
> -- Bert
Well, what can I say...I like to live on the edge.
Actually though, I had the same initial thoughts about the safety of such a
thing, but if I remember correctly, if you change the class of an object to
a class with a larger instance size and invoke a method that refers to a
variable past the end of the object, I think Squeak actually handles this
with a nice exception rather than a crash of the system...I think IIRC.
Besides, I explicitly need the functionality of changing the class to
another class with a *different* layout. My application was an object
proxying scheme. I would allocate a proxy object (the proxy class had only
on instance variable, the id of the remote object) with a size set to the
last known size of the remote object. When a message was sent to the
object, rather than doing a become (which could potentially be very
expensive) I would simply fill up the proxy object with the state from the
remote object, then change it's class to the real class...worked like champ!
Oh, and if the size was something un-expected (which was rare), I would then
fall back on become (which in most cases was fast as Dan mentions).
More information about the Squeak-dev