Implementing interfaces using alias this
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 14 07:23:24 PDT 2017
On 6/14/17 5:34 AM, 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.
>
> import std.stdio;
>
> interface IDuck
> {
> void quack();
> }
>
> class Test(T) : IDuck
> {
> T data;
> alias data this;
> }
>
> struct Duck
> {
> void quack()
> {
> writeln("Quack");
> }
> }
>
>
> void main()
> {
> Test!Duck d;
> }
An alias never causes implementation to be emitted. It should just be a
forward from one symbol name to another. You need an actual function
that can go into the vtable that takes a 'Test!Duck' as the context
pointer. This wrapper has to be written by you.
-Steve
More information about the Digitalmars-d-learn
mailing list