[Issue 20895] New: Error with alias to struct member or member function
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 4 19:52:01 UTC 2020
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 at puremagic.com
Reporter: john.michael.hall at 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);
}
--
More information about the Digitalmars-d-bugs
mailing list