D should have a root interface

JAnderson ask at me.com
Tue Jul 29 07:54:21 PDT 2008


Frank Benoit wrote:
> JAnderson schrieb:
>> 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.
>>
> 
> Right, this is an example problem from porting Java code.
> I don't like this suggestion either, but it would be better than nothing.
> I already posted so often about interface/class compatibility ... :/

Maybe we should have a standard IJavaObject :)

-Joel



More information about the Digitalmars-d mailing list