typeid() broken for interfaces?

Paulo Pinto pjmlp at progtools.org
Tue Dec 4 05:47:37 PST 2012


On Tuesday, 4 December 2012 at 13:07:59 UTC, foobar wrote:
> On Tuesday, 4 December 2012 at 12:30:29 UTC, Paulo Pinto wrote:
>> On Tuesday, 4 December 2012 at 12:26:53 UTC, Jacob Carlborg 
>> wrote:
>>> On 2012-12-04 09:22, Maxim Fomin wrote:
>>>
>>>> And what happens if nobody implements an interface?
>>>>
>>>> import std.stdio;
>>>>
>>>> interface I { }
>>>>
>>>> class A { }
>>>>
>>>> void main()
>>>> {
>>>>   I i;
>>>>   // assume this is implicit
>>>>   Object o = cast(Object)i;
>>>>   writeln(typeid(o));
>>>> }
>>>
>>> You get a segmentation fault since both "i" and "o" are null.
>>>
>>>> Safe conversion class to interface requires two conditions:
>>>> 1a) that class implements interface
>>>> 1b) if you try to use interface variable, it must be an 
>>>> allocated class
>>>> instance
>>>>
>>>> Safe conversion to Object requires:
>>>> 2a) somebody in class hierarchy implements interface
>>>> 2b) interface instance is actually allocated class instance
>>>
>>> You cannot really get an instance of an interface without 
>>> having a class implementing it. That is, without inserting 
>>> any explicit casts, which works:
>>>
>>> interface I { }
>>>
>>> class A { }
>>>
>>> void main()
>>> {
>>>   A a = new A;
>>>   I i = cast(I) a;
>>>   Object o = cast(Object)i;
>>>   writeln(typeid(a)); // A
>>> }
>>>
>>>> It is possible to check 1a) but impossible in general case 
>>>> to check 2a).
>>>> Also the first is design feature while the second is design 
>>>> abuse.
>>>
>>> I don't understand why it wouldn't be safe to allow implicit 
>>> casts of interfaces to Object.
>>>
>>> If I want to call toString, why should I need to insert an 
>>> explicit cast to Object just because I have an interface?
>>
>> If you want to call toSring, it should be part of the 
>> interface specification, as simple as that.
>>
>> Interfaces are like contracts which state what is to be 
>> expected of a certain type.
>>
>> --
>> Paulo
>
> Generally speaking you are right. But specifically regarding 
> toString, what would you suggest?
> Should each and every Interface have a toString method?

Yes for each interface where you intend to call toString().

> Should we have an IObject Interface that all classes are 
> required to explicitly inherit?
> The purpose of having a root Object class is to define the 
> _common interface of all objects_. Why else have such a class 
> in the first place?

For languages without generics where you need a common base class 
to place in containers.

> Requiring an explicit cast to Object makes sense only in a 
> no-single-root design such as the one in c++ which D sensibly 
> chose not to copy.
>
> We can have a separate debate what should the root Object 
> define, but there should not be a requirement to explicitly 
> cast any object to it.

The whole point of interfaces is to have explicit dependencies of
methods, properties, variables across the inheritance tree.

Actually there was a discussion some months ago about what 
methods still
made sense to expose via object, given D's templates.

--
Paulo



More information about the Digitalmars-d mailing list