typeid() broken for interfaces?
Paulo Pinto
pjmlp at progtools.org
Tue Dec 4 02:27:43 PST 2012
On Tuesday, 4 December 2012 at 07:38:31 UTC, Maxim Fomin wrote:
> On Monday, 3 December 2012 at 23:53:26 UTC, deadalnix wrote:
>> On Monday, 3 December 2012 at 21:53:47 UTC, Walter Bright
>> wrote:
>>> I really don't know what issue you're trying to solve here.
>>> The typeid's work fine - and interfaces are not objects.
>>> Having the typeid for an interface be an object means you
>>> cannot compare typeids of one interface to another interface.
>>>
>>
>> You can't instantiate interface. So an underlying type MUST
>> exist. The whole point of typeid on expression is to discover
>> what is the dynamic type of things, otherwize you'd be using
>> typeid(type) not typeid(expression).
>
> You cannot create interface instance with new operator because
> interface object is not valid until it is actually some class
> instance. But this does not mean neither that typeid operator
> for interfaces should return dynamic type nor that interface
> can be implicitly converted to Object - because interface
> instance may be invalid:
>
> import std.stdio;
>
> interface I { }
>
> void main()
> {
> I i;
> // should be implicitly converted
> Object o = cast(Object)i;
> writeln(typeid(o));
> }
>
> and because presence of interface does not necessarily mean
> that some class has implemented it.
>
> This makes casting interfaces to object unsafe operation that
> better should require explicit cast.
Given my experience with the interface concept in several OO
languages that implement it, I would say that you never want to
know what object was given to you.
The whole point of interfaces is to have some form of multiple
inheritance in single inheritance languages. So you *really*
don't want to cast interfaces to objects.
I can't think of a single reason this makes sense, other than
work around bad designs.
--
Paulo
More information about the Digitalmars-d
mailing list