View mode: basic / threaded / horizontal-split · Log in · Help
July 29, 2008
Re: D should have a root interface
On Tue, 29 Jul 2008 09:48:49 +0800, Neil Vice
<sardonicpresence@gmail.com> wrote:

>Robert Fraser wrote:
>> 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.
>
>Can not structs implement interfaces? Should they be implicitly castable 
>to Object?

Structs cannot implement interfaces yet and, unlike C#, D doesn't do
automatic boxing/unboxing of value types.
July 29, 2008
Re: D should have a root interface
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!!

please :(

:ahem: strong vote in favor from me.
July 29, 2008
Re: D should have a root interface
Robert Fraser wrote:
> 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.

But what if the object behind the interface is not created from D, but from C++ 
code or anything else? I'd imagine that would be a really nasty pitfall.
July 29, 2008
Re: D should have a root interface
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
July 29, 2008
Re: D should have a root interface
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 ... :/
July 29, 2008
Re: D should have a root interface
Sönke Ludwig Wrote:

> Robert Fraser wrote:
> > 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.
> 
> But what if the object behind the interface is not created from D, but from C++ 
> code or anything else? I'd imagine that would be a really nasty pitfall.

Fine, extern interfaces also shouldn't cast to object.
July 29, 2008
Re: D should have a root interface
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
August 11, 2008
Re: D should have a root interface
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
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home