Dynamically changing the shape of a class

Lex Spoon lex at cc.gatech.edu
Wed Jun 23 21:31:16 UTC 2004


Alexandre Bergel <bergel at iam.unibe.ch> wrote:
> Adding new instance variable is done by calling a become: like
> oldclassWithoutTheNewIV become: newclassWithTheNewIV
> 
> Therefore this operation requires some primitive in the VM. Is it necessary? If this mechanism is not implemented in Java is it only because the VM does not have a become: ?

This turns out to be a deep difference among languages.  The primitive
is not in Java, and it *can't* be in Java, because it completely breaks
the type system.  If you have:

	Foo x = new Foo(1,2,3);
	...
	int y = x.t;
	
And then you try to become() class Foo into Foo2, then now x is pointing
to something different than what it was pointing to when it type
checked.  It may not even have a "t" field, or if it does, it might be
of type "Integer" or "int[]" instead of type "int".

Or, try this one:

	class Foo {
		Bar x = new Bar();
		...
	}
	
Now redefine Foo as follows:

	class Foo {
		Zap x;
		...
	}
	
What do you do to update any existing Foo objects?  They all have x
instance variables that point to Bar's, but now they are supposed to
point to Zap's.  This is perfectly fine in Squeak because the language
has no discussion at all about (static) types.  In Java, however, it is
pure nonsense to even *talk* about updating Foo instances in this
fashion; in order to do it, you have add in the idea that a variable
might point to something of a different type than is declared, and at
that point you have dramatically changed the language.  (Or, you have to
remove the static types, which is entirely possible....)

Updating programs as they run is a very useful feature, but it requires
that the language supports it.  It seems quite easy to design a language
and accidentally make this feature difficult or impossible.


-Lex



More information about the Squeak-dev mailing list