Delegation of interfaces

Mahe maheweb at web.de
Thu Jul 19 10:16:49 PDT 2007


Hi,

I like multi inheritance, but all modern programming languages prohibit it. So I accept single inheritance. But with single inheritance you often delegate interface implementation to some other internal objects of the class. Only some special functions of the interface are threaded explicitly. So I think this delegation could be automated.

For example

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

class ArrayList : I List
{
	void add( object o )
	{
	 	/* something */
	}
	void remove( object o)
	{
	 	/* something */
	}
	int size()
	{
	 	/* something */
	}
}

}

class Componet
{
	/* something */
}

class ComponetWithSubComponent : Componet  , IList
{
	/* something*/
	ArrayList internalList;

	void add( object o )
	{
		/* check if o is an legal argument,
	  else throw an exception  */
	...
	...
	internalList.add( o );
	}

	void remove( object o)
	{
		internalList. remove ( o );
	}

	int size()
	{
		return internalList. size ( );
	}		
}

 
Here the functions "ComponetWithSubComponent.remove( object o )" and "ComponetWithSubComponent.size( object o )" are simple delegations to the member "internalList". It would be nice if you can leave out the function "remove" and "size" in "ComponetWithSubComponent" and only write something like

delegate interface  IList to internalList

So, all interface functions of "IList" that are not defined in class "ComponetWithSubComponent" are automatically delegated to "internalList".
So also compiler optimizations are possible.




More information about the Digitalmars-d mailing list