Restriction on interface function types

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 12 06:45:31 PDT 2014


On Wed, 12 Mar 2014 09:34:32 -0400, Steve Teale  
<steve.teale at britseyeview.com> 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  
>> <steve.teale at britseyeview.com> 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.

The type cannot be determined at runtime, it's a static language.

foreach(i; arr) { typeof(i.myType()) x = cast(i.myType()) i; x.foo();}

What is typeof(x)? It needs to be decided at compile time.

-Steve


More information about the Digitalmars-d mailing list