www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D should have a root interface

reply Frank Benoit <keinfarbton googlemail.com> writes:
// 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.
Jul 28 2008
next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
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.
Jul 28 2008
next sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
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!
Jul 28 2008
next sibling parent downs <default_357-line yahoo.de> writes:
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!

please :( :ahem: strong vote in favor from me.
Jul 28 2008
prev sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
Aug 11 2008
prev sibling next sibling parent reply Neil Vice <sardonicpresence gmail.com> writes:
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?
Jul 28 2008
next sibling parent Frank Benoit <keinfarbton googlemail.com> writes:
Neil Vice schrieb:
 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 are plain data. They do not have a virtual function table. So they are neither compatible to interfaces and not to classes.
Jul 28 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Neil Vice" <sardonicpresence gmail.com> wrote in message 
news:g6lstt$2qi4$1 digitalmars.com...

 Can not structs implement interfaces? Should they be implicitly castable 
 to Object?

It's not possible, but I swear I remember W mentioning this sometime. I can't seem to find when he said it though. It might be something that's still in development for D2, but I also remember him saying something about structs that implement interfaces wouldn't be able to be cast to those interface references, so I'm not sure what purpose it'd serve.
Jul 28 2008
next sibling parent JAnderson <ask me.com> writes:
Jarrett Billingsley wrote:
 "Neil Vice" <sardonicpresence gmail.com> wrote in message 
 news:g6lstt$2qi4$1 digitalmars.com...
 
 Can not structs implement interfaces? Should they be implicitly castable 
 to Object?

It's not possible, but I swear I remember W mentioning this sometime. I can't seem to find when he said it though. It might be something that's still in development for D2, but I also remember him saying something about structs that implement interfaces wouldn't be able to be cast to those interface references, so I'm not sure what purpose it'd serve.

Check out the D conference Part 2 of Walter and Andrei talk (at 34m). http://d.puremagic.com/conference2007/speakers.html Its mainly for templates though. Kinda concept like. -Joel
Jul 28 2008
prev sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
== Quote from Jarrett Billingsley (kb3ctd2 yahoo.com)'s article
 "Neil Vice" <sardonicpresence gmail.com> wrote in message
 news:g6lstt$2qi4$1 digitalmars.com...
 Can not structs implement interfaces? Should they be implicitly castable
 to Object?

can't seem to find when he said it though. It might be something that's still in development for D2, but I also remember him saying something about structs that implement interfaces wouldn't be able to be cast to those interface references, so I'm not sure what purpose it'd serve.

It was a really hacky way to implement contracts for D2. I don't think we'll ever see this feature. Sean
Jul 28 2008
parent Sean Kelly <sean invisibleduck.org> writes:
== Quote from Sean Kelly (sean invisibleduck.org)'s article
 == Quote from Jarrett Billingsley (kb3ctd2 yahoo.com)'s article
 "Neil Vice" <sardonicpresence gmail.com> wrote in message
 news:g6lstt$2qi4$1 digitalmars.com...
 Can not structs implement interfaces? Should they be implicitly castable
 to Object?

can't seem to find when he said it though. It might be something that's still in development for D2, but I also remember him saying something about structs that implement interfaces wouldn't be able to be cast to those interface references, so I'm not sure what purpose it'd serve.

think we'll ever see this feature.

Oops, I meant concepts, not contracts. Sean
Jul 28 2008
prev sibling parent Max Samukha <samukha voliacable.com.removethis> writes:
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.
Jul 28 2008
prev sibling next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Tue, Jul 29, 2008 at 10:48 AM, Neil Vice <sardonicpresence gmail.com>wrote:

 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?

No, structs cannot implement interfaces, and it makes no sense to cast a struct to Object either implicitly or explicitly. Structs in D are for representing value types and have no virtual methods or inheritance. --bb
Jul 28 2008
prev sibling parent reply =?ISO-8859-15?Q?S=F6nke_Ludwig?= writes:
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.
Jul 28 2008
parent Robert Fraser <fraserofthenight gmail.com> writes:
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.
Jul 29 2008
prev sibling parent reply JAnderson <ask me.com> writes:
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
Jul 29 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
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 ... :/
Jul 29 2008
parent JAnderson <ask me.com> writes:
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
Jul 29 2008