View mode: basic / threaded / horizontal-split · Log in · Help
October 21, 2006
interface reference not compatible to Object?
interface I{
   void func();
}
class C : I {
   void func(){
   }
}
void main(){
   C c = new C;
   c.toHash(); // from class Object
   I i = c;
   i.toHash(); // line 14
}
/////////////////////////////
t.d(14): no property 'toHash' for type 't.I'
t.d(14): function expected before (), not 1 of type int


Is this the intended behaviour? If *every* object in D is a Object, then
every interface reference refers to an Object also. This said, the above
should compile?
October 21, 2006
Re: interface reference not compatible to Object?
As already said in #d, this one compiles:

interface IObject {
   void print();
   char[] toString();
   hash_t toHash();
   int opCmp(Object o);
   int opEquals(Object o);
}

interface I : IObject {
   void func();
}

class C : I {
   void func(){
   }
}

void main(){
   C c = new C;
   c.toHash();
   I i = c;
   i.toHash();
}

..without given errors. Might be an option to add it into object.d or
such.

Best regards,
Alex

On Sat, 2006-10-21 at 11:44 +0200, Frank Benoit (keinfarbton) wrote:
> interface I{
>     void func();
> }
> class C : I {
>     void func(){
>     }
> }
> void main(){
>     C c = new C;
>     c.toHash(); // from class Object
>     I i = c;
>     i.toHash(); // line 14
> }
> /////////////////////////////
> t.d(14): no property 'toHash' for type 't.I'
> t.d(14): function expected before (), not 1 of type int
> 
> 
> Is this the intended behaviour? If *every* object in D is a Object, then
> every interface reference refers to an Object also. This said, the above
> should compile?
October 21, 2006
Re: interface reference not compatible to Object?
>> Is this the intended behaviour? If *every* object in D is a Object, then
>> every interface reference refers to an Object also. This said, the above
>> should compile?

And as already said in #d : I wonder why this even compiles. Why is C
not enforced to "reimplement" the methods from IObject?

Wouldn't it be consistent if the compiler implicitly inherit all
interfaces without a super-interface from this IObject?
October 21, 2006
Re: interface reference not compatible to Object?
Frank Benoit (keinfarbton) wrote:
>>> Is this the intended behaviour? If *every* object in D is a Object, then
>>> every interface reference refers to an Object also. This said, the above
>>> should compile?
> 
> And as already said in #d : I wonder why this even compiles. Why is C
> not enforced to "reimplement" the methods from IObject?

I guess because the interface only requires the methods to be 
implemented, but allows them to be implemented in a base class as well 
as the class itself.

> Wouldn't it be consistent if the compiler implicitly inherit all
> interfaces without a super-interface from this IObject?

Maybe Object itself as well?
That way functions can accept any object (whether referenced by class or 
interface) as an IObject.

Though I would prefer it if all interface references would just be 
implicitly convertible to Object. Java does this, IIRC.
October 21, 2006
Re: interface reference not compatible to Object?
Frits van Bommel wrote:
> Frank Benoit (keinfarbton) wrote:
>> Wouldn't it be consistent if the compiler implicitly inherit all
>> interfaces without a super-interface from this IObject?
> 
> Maybe Object itself as well?
> That way functions can accept any object (whether referenced by class or
> interface) as an IObject.
> 
> Though I would prefer it if all interface references would just be
> implicitly convertible to Object. Java does this, IIRC.

AFAIK it's the basis for developing high level software architectures in
languages that lack multiple inheritance of objects but allow multiple
inheritance of interfaces, e.g. Java.

It's pretty cool that someone came up with this in #d, but seriously irc
is not the best place for documenting things. This information should be
in the official documentation.

The official documentation clearly tells how you can write classes that
implement interfaces and that the compiler bugs you, if the class does
not implement all abstract methods. This is very good, but the
documentation is dead silent about dereferencing objects via interfaces.
I guess it's because Walter is mainly a C++ guy and does not yet
understand how interfaces work in Java. D has the same mechanism for
interfaces as Java does. Why then should interfaces in D work like they
work in C++, i.e. they do not work because C++ does not have any interfaces!
October 21, 2006
Re: interface reference not compatible to Object?
On Sat, 21 Oct 2006 15:11:59 +0200, Frits van Bommel
<fvbommel@REMwOVExCAPSs.nl> wrote:

>Frank Benoit (keinfarbton) wrote:
>>>> Is this the intended behaviour? If *every* object in D is a Object, then
>>>> every interface reference refers to an Object also. This said, the above
>>>> should compile?
>> 
>> And as already said in #d : I wonder why this even compiles. Why is C
>> not enforced to "reimplement" the methods from IObject?
>
>I guess because the interface only requires the methods to be 
>implemented, but allows them to be implemented in a base class as well 
>as the class itself.
>
>> Wouldn't it be consistent if the compiler implicitly inherit all
>> interfaces without a super-interface from this IObject?
>
>Maybe Object itself as well?
>That way functions can accept any object (whether referenced by class or 
>interface) as an IObject.
>
>Though I would prefer it if all interface references would just be 
>implicitly convertible to Object. Java does this, IIRC.

The problem is that not all objects in D are D objects. An interface
may be a com interface that cannot be cast to Object or inherit from
IObject. It was discussed somewhere in the NG, IIRC
October 21, 2006
Re: interface reference not compatible to Object?
Max Samuha wrote:
> On Sat, 21 Oct 2006 15:11:59 +0200, Frits van Bommel
> <fvbommel@REMwOVExCAPSs.nl> wrote:
> 
>> Frank Benoit (keinfarbton) wrote:
>>> Wouldn't it be consistent if the compiler implicitly inherit all
>>> interfaces without a super-interface from this IObject?
>> Maybe Object itself as well?
>> That way functions can accept any object (whether referenced by class or 
>> interface) as an IObject.
>>
>> Though I would prefer it if all interface references would just be 
>> implicitly convertible to Object. Java does this, IIRC.
> 
> The problem is that not all objects in D are D objects. An interface
> may be a com interface that cannot be cast to Object or inherit from
> IObject. It was discussed somewhere in the NG, IIRC

Can non-COM interfaces be implemented by COM objects? (I'm pretty sure 
they at least can't be implemented by non-d objects, so does it matter?)

Since whether an interface is a COM interface can easily determined 
(they inherit from std.c.windows.com.IUnknown) can't we at least make it 
so that all non-COM interfaces are convertible to Object?
October 21, 2006
Re: interface reference not compatible to Object?
Frank Benoit (keinfarbton) wrote:
> interface I{
>     void func();
> }
> class C : I {
>     void func(){
>     }
> }
> void main(){
>     C c = new C;
>     c.toHash(); // from class Object
>     I i = c;
>     i.toHash(); // line 14
> }
> /////////////////////////////
> t.d(14): no property 'toHash' for type 't.I'
> t.d(14): function expected before (), not 1 of type int
> 
> 
> Is this the intended behaviour? If *every* object in D is a Object, then
> every interface reference refers to an Object also.
> This said, the above should compile?

Replace:
	i.toHash();
with:
	(cast(Object)i).toHash();
and it will work, *provided* that i is actually an instance of an 
Object. If it is not, for example if it came from some COM DLL, the cast 
will fail.
October 22, 2006
Re: interface reference not compatible to Object?
Walter Bright wrote:
> Frank Benoit (keinfarbton) wrote:
>> Is this the intended behaviour? If *every* object in D is a Object, then
>> every interface reference refers to an Object also.
>> This said, the above should compile?
> 
> Replace:
>     i.toHash();
> with:
>     (cast(Object)i).toHash();
> and it will work, *provided* that i is actually an instance of an
> Object. If it is not, for example if it came from some COM DLL, the cast
> will fail.

Is there much hope that the compiler will make the cast implicitly in
the future releases?
October 23, 2006
Re: interface reference not compatible to Object?
On Sat, 21 Oct 2006 21:54:03 +0200, Frits van Bommel
<fvbommel@REMwOVExCAPSs.nl> wrote:

>Max Samuha wrote:
>> On Sat, 21 Oct 2006 15:11:59 +0200, Frits van Bommel
>> <fvbommel@REMwOVExCAPSs.nl> wrote:
>> 
>>> Frank Benoit (keinfarbton) wrote:
>>>> Wouldn't it be consistent if the compiler implicitly inherit all
>>>> interfaces without a super-interface from this IObject?
>>> Maybe Object itself as well?
>>> That way functions can accept any object (whether referenced by class or 
>>> interface) as an IObject.
>>>
>>> Though I would prefer it if all interface references would just be 
>>> implicitly convertible to Object. Java does this, IIRC.
>> 
>> The problem is that not all objects in D are D objects. An interface
>> may be a com interface that cannot be cast to Object or inherit from
>> IObject. It was discussed somewhere in the NG, IIRC
>
>Can non-COM interfaces be implemented by COM objects? (I'm pretty sure 
>they at least can't be implemented by non-d objects, so does it matter?)
>
Don't really know. Never tried that.

>Since whether an interface is a COM interface can easily determined 
>(they inherit from std.c.windows.com.IUnknown) can't we at least make it 
>so that all non-COM interfaces are convertible to Object?

One more special case rule in the language. Though I wouldn't mind if
it was there.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home