Implementing interfaces using alias this

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 14 04:40:02 PDT 2017


Balagopal Komarath wrote:

> On Wednesday, 14 June 2017 at 09:41:49 UTC, ketmar wrote:
>> Balagopal Komarath wrote:
>>
>>> Why doesn't this work? The Test!Duck type has a void quack() method but 
>>> the compiler says it is not implemented.
>>
>> 'cause `alias this` is *not* a tool that can be used to emulate 
>> inheritance. no, `quack` is NOT impemented. `alias this` won't 
>> automagically paste the code.
>
> Thanks for the reply. Is there any reason for disallowing this? AFAIK, 
> the alias this guarantees that the interface provided by Test!T is a 
> superset of the interface provided by T. And, when T = Duck, T provides 
> everything required by IDuck. Couldn't the compiler check while 
> instantiating that Test!Duck provides all methods required by IDuck?

besides improper using of `alias this`, there is a technical reason too: 
`struct` is not a `class`, it doesn't have VMT, and other class goodies. as 
i said, `alias this` is not a macro, it doesn't paste code, it just tells 
the compiler to make "symbol redirection" on typechecking. interfaces 
*require* a full-featured class, with VMT, and some hidden pointers to 
support hidden interface machinery.


More information about the Digitalmars-d-learn mailing list