D should have a root interface

Bruno Medeiros brunodomedeiros+spam at com.gmail
Mon Aug 11 08:52:02 PDT 2008


Frank Benoit wrote:
> Robert Fraser 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.
>>
>> Or, you know, just make all instances of interfaces that don't extend 
>> IUnknown implicitly castable to object.
> 
> yes yes yes yes!
> 

Yes, and this would be a much better solution than having a root 
interface. :)

Frankly, I was quite surprised to find out only now that interfaces were 
not implicitly castable to Object. I knew they were not binary 
compatible with each other (as they are in Java), but I always thought 
them to be implicitably castable (whenever type-safe).

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list