On Wednesday, 12 March 2014 at 13:34:33 UTC, Steve Teale wrote:
On Wednesday, 12 March 2014 at 13:12:20 UTC, Steven Schveighoffer wrote:
On Wed, 12 Mar 2014 09:05:05 -0400, Steve Teale <[email protected]> wrote:

How is the compiler to build it's one copy of bad? Should x be typed as A or B? Or something not even seen in this module that could derive from I?

-Steve

Let's take bad() away, and instead:

class A : I
{
   A myType() { return cast(A)null;}
   final void foo();
}

class B : I
{
   B myType() {return cast(B) null;}
   final void bar();
}

void main()
{
   I[] arr = [new A, new B];
   foreach(i; arr) { (cast(typeof(i.myType()) i).foo() }
}

myType() is a virtual function, so calling it through the interface type should get the correct version right?, and then the cast should cause a call to A or B.

It will *call* the correct version, but the signature used will still statically be the interface's signature.

It can make a difference when you *statically* know you are in a derived type:

I i = new A();
A a = new A();

I ii = i.myType();
A aa = a.myType();

Here, the call to "myType", in both cases, will "runtime" resolve to A.myType(). *However*, the static type used to return the value, will not be the same.

Reply via email to