Delegation of interfaces

BCS ao at pathlink.com
Thu Jul 19 10:33:29 PDT 2007


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.





More information about the Digitalmars-d mailing list