How difficult is multiple inheritance?

Vassili Bykov vassili at magma.ca
Sun Jan 3 00:03:19 UTC 1999


From: Stefan Matthias Aust <sma at kiel.netsurf.de>
>I wondered how easy (or difficult) it would be to add support for multiple
>superclasses (aka parents) to Squeak. I think, there's just one method
>Interpreter>>lookupMethodInClass: which would be affected by the change.


It is not that simple, at least not for the general case (i.e. with no
limitations on the superclasses).  The trickiest thing is the instance
layout.  Traditionally, instance variable access is compiled as direct
indexing into an instance.  MI requires more hoops to jump.  Here is an
example:

Suppose you have two subclasses of Object: A, with an instance variable a;
and B, with an instance variable b.  Access to both these variables in
methods of A and B is compiled as accessing an instance variable at index 1.
In instances of C, one of the two inherited variables ends up at index 2,
meaning the compiled methods of the class it comes from cannot be used.

Perhaps the simplest image-level solution would be to copy the inherited
compiled methods into the subclass'es own method dictionary, adjusting the
instance variable indices in the methods.  (This "method propagation" also
has to be done later as the superclass's methods are added or modified).

Alternative solutions would need the VM support.  One possibility would be
for instance variable access to use, instead of an absolute index into an
instance, an index relative to some base value.  The base would be
determined at the runtime depending on the actual class of the instance, and
on the class where the method that accesses the variable was defined.

--Vassili

--
Vassili Bykov
The Object People
www.objectpeople.com





More information about the Squeak-dev mailing list