Delegation of interfaces

Mahe maheweb at web.de
Fri Jul 20 02:00:19 PDT 2007


Nice solution BSC,
it even works when you want to add some functionality, e.g. in the “add” function:

module main;

import std.stdio;

int main()
{
    IList l;
    l = new ArrayList();
    l.add( null );

    writefln(  );

    l = new ComponetWithSubComponent();
    l.add( null );

    getch();
    return 0;
}

interface IList
{
    void add( Object o );
    void remove( Object o);
    int size();
}

class ArrayList : IList
{
    void add( Object o )
    {
         writefln( "ArrayList.add" );
    }
    void remove( Object o)
    {
         writefln( "ArrayList.remove" );
    }
    int size()
    {
         writefln( "ArrayList.size" );
         return 0;
    }
}

class Componet
{
    /* something */
}

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
{
    this()
    {
        internalList = new ArrayList();
    }

    public void add( Object o )
    {
        writefln( "ComponetWithSubComponent.add" );
        internalList.add( o );
    }

    ArrayList internalList;
    mixin IListImp!(internalList);
}


But it only works if you write the new “add” function before “mixin IListImp!(internalList) listMixin;” 

class ComponetWithSubComponent : Componet , IList
{
    this()
    {
        internalList = new ArrayList();
    }

    ArrayList internalList;
    mixin IListImp!(internalList);

    public void add( Object o )
    {
        writefln( "ComponetWithSubComponent.add" );
        listMixin.add( o );
    }  
}

This does not work – bug or intended  ?




More information about the Digitalmars-d mailing list