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.

I don't think it has to do with pasting code.

d.Quack() is well defined through the alias. Inheritance requires that a Quack() exists, and it does, through the alias.

The compiler could easily create an implementation wrapper that uses the alias this.

I believe it is simply because the logic is not implement in the compiler, not because there is some logic issue in it.
import std.stdio;

interface IDuck
{
    void quack();
}

class Test(T) : IDuck
{
    T data;
    alias data this;
    void quack() { data.quack(); }
}

struct Duck
{
    void quack()
    {
        writeln("Quack");
    }
}


void main()
{
        Test!Duck d;
        d.quack();
}

which, unfortunately causes a segmentation fault ;)(from dpaste) (even if you remove IDuck) https://dpaste.dzfl.pl/69ddb3f2b1e9

The main issue is that the compiler would have to parse the alias and see if it has members that are to be used, not hard... but this is probably not standard behavior and might lead to other problems down the road(specially with multiple alias this). Basically, which quack to call? I think it's better to require it to be explicit(as I have tried to do).



Reply via email to