[Issue 14612] typeid(interface) returns TypeInfo_Class object

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jul 9 23:27:38 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14612

--- Comment #4 from Rainer Schuetze <r.sagitario at gmx.de> ---
> And you can do the latter behavior by typeid(cast(Object)obj).
> I feel that syntactic cost is enough small.

cast(Object) creates a call into the runtime (_d_interface_cast) and is pretty
expensive. It could be optimized to the series of indirections by the compiler,
though.

> In short, the orthogonality may have been priority than the intuitive, but complex behavior.

The "complex" behaviour costs just an additional reading of an offset to the
object base address, very much what a thunk does when calling a virtual
function.

I guess the current behaviour could be described as "typeid(I) evaluates to the
most derived interface that is implemented by the object and that inherits from
the static type."

But "most derived" is not well defined when it comes to inheriting from
multiple interfaces:

import std.stdio;

interface I {}
interface J : I {}
interface K : I {}
class E : J, K {}

void main()
{
    E e = new E;
    J j = e;
    K k = e;
    I ij = j;
    I ik = k;
    writeln(typeid(ij));      // prints 'test.J' !?
    writeln(typeid(ik));      // prints 'test.K' !?
}

--


More information about the Digitalmars-d-bugs mailing list