www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20895] New: Error with alias to struct member or member

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

          Issue ID: 20895
           Summary: Error with alias to struct member or member function
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: john.michael.hall gmail.com

The code below has two unittests. The first one fails to compile and generates
an error "this for foo needs to be type Foo not type Bar". A similar error
would be given to do the same thing with an alias to the struct member.

The second one has broadly the same functionality, but replaces the structs
with classes and successfully compiles. 

Ideally there would be some kind of workaround for structs. For instance, 
alias x.foo this; 
fails to compile but would be a syntax consistent with other D features.

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

    static struct Bar
    {
        Foo x;

        alias bar = x.foo;
    }

    Bar x = Bar(Foo(1));

    assert(x.bar == 2);
}

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);}

        alias bar = foo;
    }

    Bar x = new Bar(1);

    assert(x.bar == 2);
}

--
Jun 04 2020