Assigning Interface to Object
Steven Schveighoffer
schveiguy at yahoo.com
Mon Jan 17 05:09:27 PST 2011
On Sun, 16 Jan 2011 08:11:45 -0500, Stewart Gordon <smjg_1998 at yahoo.com>
wrote:
> On 16/01/2011 08:23, "Jérôme M. Berger" wrote:
>> Stewart Gordon wrote:
>>> On 15/01/2011 17:44, Steven Schveighoffer wrote:
>>> <snip>
>>>> Which unnecessarily complicates things. For example, you can't compare
>>>> two interfaces (try it!).
>>>
>>> ?
>> interface I {}
>>
>> ...
>>
>> I a = ...;
>> I b = ...;
>>
>> if (a == b) //<-- ERROR
>
>
> 1.065: compiles without error, though it seems to be equivalent to is
> 2.051: it's really weird
> ----------
> C:\Users\Stewart\Documents\Programming\D\Tests>dmd interface_equals.d
> interface_equals.d(7): Error: function object.opEquals (Object lhs,
> Object rhs)
> is not callable using argument types (I,I)
> interface_equals.d(7): Error: cannot implicitly convert expression (a)
> of type i
> nterface_equals.I to object.Object
> interface_equals.d(7): Error: cannot implicitly convert expression (b)
> of type i
> nterface_equals.I to object.Object
> ----------
>
> Of course, if the interface declares an opEquals, it's a whole different
> story....
Nope. try it:
interface I { bool opEquals(I other); }
I a;
I b;
a == b; // same error.
The problem is that when TDPL was implemented, (Object)a == (Object)b was
redefined from a.opEquals(b) to object.opEquals(a, b), where object is the
module object.
That function's signature looks like this:
bool opEquals(Object lhs, Object rhs);
For some reason the compiler tries to do the same thing with interfaces,
but of course, there is no opEquals for your specific interface in
object. Even if there was, you'd likely get an overload error.
This would be easily resolved if interfaces were known to be Objects.
-Steve
More information about the Digitalmars-d-learn
mailing list