D should have a root interface

JAnderson ask at me.com
Tue Jul 29 01:13:09 PDT 2008


Frank Benoit wrote:
> // user lib
> interface List{
>     void add(Object);
> }
> interface ArrayList : List{
>     void add(Object){}
> }
> 
> // calling code
> List list = ...
> IFile file = ... // interface type
> list.add( file ); // compile error
> list.add( cast(Object) file );
> 
> This makes problems when one want to add() an interface instance. A cast 
> to Object is necessary. This is so ugly.
> 
> If there would be at least an empty root interface for all D interfaces, 
> this can be used for method signatures.
> 
> // D runtime and used by compiler
> interface IObject {
> }
> 
> // user lib
> interface List{
>     void add(Object);
>     void add(IObject);
> }
> interface ArrayList : List{
>     void add(Object){ ... }
>     void add(IObject io){ add(cast(Object)io); }
> }
> 
> This would not break any code and helps to make the calling code less 
> casty.

Personally I think its a mistake to rely on such a low level interface. 
     I think lists should be done with templates and less general 
interfaces.  I'm not sure that D should be encouraging dangerous 
practices like this less we end up in early C# / Java  land.

The only reason for doing something like this I can think of is if your 
doing some sort of string print out.  Even a factory is better 
represented in terms of an interface that describes how it will be used. 
  Otherwise some sort of downcast will be needed to pull the object out 
of the list.  Ideally you should never need to downcast.

-Joel



More information about the Digitalmars-d mailing list