D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20895 - Error with alias to struct member or member function
Summary: Error with alias to struct member or member function
Status: RESOLVED DUPLICATE of issue 6842
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-04 19:52 UTC by John Hall
Modified: 2023-02-08 20:17 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description John Hall 2020-06-04 19:52:01 UTC
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);
}
Comment 1 John Hall 2023-02-08 20:17:15 UTC
This appears to be related to another known bug.

*** This issue has been marked as a duplicate of issue 6842 ***