Autodelegation: A Multiple-Inheritance Alternative

While working in .NET and Java, on (admittedly rare) occasions I’ve wanted multiple-inheritance. Or I’ve had an implementation of an interface that I would like to extend, but the implementation was final. Essentially there have been times I’ve wanted to incorporate some existing functionality into a new class where inheritance seemed the most natural way but couldn’t. Something I would like to see in a language is what I call “autodelegation”.

The easiest  way to explain it is probably by way of an example. Suppose you have something like this:
(Sorry for the formatting – this blog software doesn’t have very good code support and keeps killing my indentations.)

public interface Reader
{
public void readit();
}

public interface Writer
{
public void writeit();
}

public class ReaderImpl implements Reader
{
public void readit() {}
}

public class WriterImpl implements Writer
{
public void writeit() {}
}

Now let’s say we want a single class that implements both, but we want to reuse the functionality in BOTH ReaderImpl and WriterImpl. This is a place where multiple inheritance might make sense. But since Java doesn’t support it, we can’t do that. So about the only thing we can do is set up some kind of delegation.

public class ReaderWriterImpl implements Reader, Writer
{
private Reader reader = new ReaderImpl();
private Writer writer = new WriterImpl();

public void readit()
{
reader.readit();
}

public void writeit()
{
writer.writeit();
}

}

That isn’t too bad for this simple example, but it rapidly gets out of hand. You may be able to extend one of the implementers and save some work, but it still isn’t very clean. With autodelegation, the idea is you would have another modifier for fields that define an object to delegate an interface’s calls to. Something like:

public class ReaderWriterImpl implements Reader, Writer
{
private delegate Reader reader = new ReaderImpl();
private delegate Writer writer = new WriterImpl();
}

Note that this is different from .NET delegates. This could just be syntactic sugar, with the compiler generating code very similar to the first example. You could also override individual methods from the interfaces as needed. There would need to be some restrictions probably. For example, the delegates should be treated as final and as such must be initialized either in the declaration or the constructor and there could be at most one delegate for each implemented interface.

I don’t know if any languages out there actually implement this kind of construct. Most of my experience has been with .NET and Java. You can probably get close to it in Java with Dynamic Proxies, but that seems messy and slow for something that should be able to be done by the compiler. But if there are implementations like this I’d love to hear about them.

This entry was posted in General. Bookmark the permalink.

1 Response to Autodelegation: A Multiple-Inheritance Alternative

  1. KevinRichey says:

    Alas, the numerous occasions I have longed for this feature… at one time I had something kinda working in C++ using template and macro black magic.

Leave a Reply