Suggestion: Walter Bright, why not to implement multiple inheritance in D?

Burton Radons burton-radons at smocky.com
Sat Dec 2 06:08:14 PST 2006


As an aside to Walter, I stumbled on a solution to the multiple vtable 
problem a couple years ago. Instead of putting the vtable in the object, 
you put it in the reference, like with a delegate. Then if an object is 
posing as a base type, you just select an appropriate vtable and offset 
the pointer. That way you don't need to worry about trying to fit 
vtables into base types, although you still need to put offsets into the 
vtable for mutual base types in the (A; B : A; C : A; D : B, C) case for 
field pointers. Didn't say it was a panacea.

It's no good for D, though. Too bad because (even without MI) it would 
easily allow other functionality. There's no reason then to keep value 
and reference types separate, for example, and a mostly-equivalent 
vtable can be selected based upon contextual needs. I had an example for 
why you'd want to do that around but I can't remember it now.

Ah, here's one: say you take a reference of a middle value in an array. 
If someone tries to delete this reference, you can either delete the 
whole array (bad), sigsegv (bad), ignore it because it's not a pointer 
to the beginning of the block (bad because if it were a reference to the 
first array element it would still be nonsensical to delete it but it 
would work), ignore it because it's a reference to an array element 
(better, and possible with the scheme here), or remove it from the array 
(which is probably best but depends upon storing the length with the 
array data, which might be more correct overall but has too many bad 
points to counter its few good ones. But if arrays WERE implemented like 
that, this scheme would allow that to happen efficiently).

It'd remove the need for boxing, too, since you could just convert a 
value into a reference, cast it to void&, then try to cast it back into 
a value type.

It's a little faster on dispatch, of course; one less pointer to follow. 
Likely a little slower when all's considered.

Oh well, stuff for my language, if I ever get around to that.



More information about the Digitalmars-d mailing list