https://issues.dlang.org/show_bug.cgi?id=20233

John Hall <john.michael.h...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.michael.h...@gmail.com

--- Comment #8 from John Hall <john.michael.h...@gmail.com> ---
Just ran into this.

I have two examples below. The first is with structs and alias this and the
second is with classes. The class example compiles without error, but the
struct one fails to compile. How classes behave is how I would expect structs
to, and I was surprised when they didn't. 

Consider also the diamond problem with multiple inheritance. The way classes
work in D is that there is only one way to call a member or member function.
Thus, opDispatch acts like overriding any base member function calls. alias
this works this way most of the time in that a function in the derived struct
will override one in the base struct. However, the doesn't hold with
opDispatch.

unittest
{
    static struct Foo
    {
        int value;
        int foo() {
            return value + 1;  
        }
    }

    static struct Bar
    {
        Foo x;
        alias x this;

        int opDispatch(string op)()
            if (op == "bar")
        {
            return x.foo;
        }
    }

    Bar x = Bar(Foo(1));

    assert(x.bar == 2);
    assert(x.foo == 2);
    assert(x.value == 1);
}


unittest
{
    static class Foo
    {
        int value;
        this(int x) {
            value = x;   
        }
        int foo() {
            return value + 1;  
        }
    }

    static class Bar : Foo
    {
        this(int x) {
            super(x);   
        }

        int opDispatch(string op)()
            if (op == "bar")
        {
            return foo;    
        }
    }

    Bar x = new Bar(1);
    assert(x.foo == 2);
    assert(x.bar == 2);
    assert(x.value == 1);
}

--

Reply via email to