Many texts refer to the Liskov Substitution Principle (or LSP) which states, rather more elegantly than I am about to, that inheritance should only be used if an instance of the subclass is, to all intents and purposes, an instance of the base class. Where this is not the case, composition and delegation should be preferred. However, none of the languages I've come across have any "syntactic sugar" that makes it easy to do so. The closest thing that springs to mind is the following C++:

class Derived : private Base
{
public:
    using Base::someMethod();
};

This has the obvious drawback of actually using inheritance, which we were trying to avoid in the first place, which in turns leads to "weakening" the LSP only to apply to public inheritance.

There is also the situation when you want to draw functionality from more than one other class, but only have single inheritance at your disposal.

With C# having an (almost) clean slate to start from, metadata being so abundant in .NET languages, and reflection also possible in Java, I'm sure it would be possible to make it somewhat easier to use composition. Some approaches that spring to mind are:

  • attributes applied to member variables which instruct the compiler which methods can be delegated to
  • a composedOf or delegatesTo keyword which would function in a similar way to Java's implements
  • a similar keyword to that above, but used in the same way as a C# property declaration

Does anyone else have any other ideas?