Bug 20895 – Error with alias to struct member or member function
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-06-04T19:52:01Z
Last change time
2023-02-08T20:17:15Z
Assigned to
No Owner
Creator
John Hall
Comments
Comment #0 by john.michael.hall — 2020-06-04T19:52:01Z
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 by john.michael.hall — 2023-02-08T20:17:15Z
This appears to be related to another known bug.
*** This issue has been marked as a duplicate of issue 6842 ***