Delegation of interfaces

Regan Heath regan at netmail.co.nz
Fri Jul 20 01:03:01 PDT 2007


BCS wrote:
> You can plug functionality into a class with a mixin. This avoids having 
> a inner class just to implement an interface.
> 
> 
> |interface IList
> |{
> |    void add( object o );
> |    void remove( object o);
> |    int size();
> |}
> |
> |template IListImp()
> |{
> |    /* somthing */
> |    void add( object o )
> |    {
> |         /* something */
> |    }
> |    void remove( object o)
> |    {
> |         /* something */
> |    }
> |    int size()
> |    {
> |         /* something */
> |    }
> |}
> |
> |class ArrayList : IList
> |{
> |    mixin IListImp!();
> |}
> |
> |class Componet
> |{
> |    /* something */
> |}
> |
> |class ComponetWithSubComponent : Componet , IList
> |{
> |    mixin IListImp!();
> |}
> 
> also the direct chaining can be bundled up like this:
> 
> |template IListImp(alias inner)
> |{
> |    void add( object o )
> |    {
> |         inner.add(o)
> |    }
> |    void remove( object o)
> |    {
> |         inner.remove(o)
> |    }
> |    int size()
> |    {
> |         return inner.size()
> |    }
> |}
> |
> |class ComponetWithSubComponent : Componet , IList
> |{
> |    ArrayList internalList;
> |    mixin IListImp!(internalList);
> |}
> 
> what I want is to be able to make a template the would be able to 
> implement any interface by getting a list of functions from the type.
> 
> DMD should be able to inline some of this away even now.
> 
> If that isn't what you are looking for, then I'm missing something.

Those are some very clever solutions, I reckon they need to go on a wiki 
or something.

That said, I wonder if there is some merit in having syntactic sugar in 
the compiler, in the form suggested by the OP to implement what would 
essentially be the 2nd solution you gave above?

In short, should this be a feature?  We could continue D's tradition and 
re-use the 'delegate' keyword! ;)

Regan



More information about the Digitalmars-d mailing list