Code:
------
import std.typecons : TypeTuple;
struct S {
int x;
int y;
}
struct T {
S s;
alias s this;
}
static assert(is(typeof(T.init.x)));
static assert(is(typeof(T.init.y)));
struct U {
T t;
//alias x = t.x; // <-- FAIL
alias x = t.s.x; // <-- OK
}
void main() {
}
------
The line marked "FAIL" does not compile if uncommented:
------
test.d(18): Error: need 'this' for 's' of type 'S'
------
The following line works. But it should not be necessary to specify '.s' here, since the static asserts before the definition of struct U show that '.x' and '.y' can be accessed directly given an instance of T, due to the `alias s this`. So it should be possible to alias the symbol directly without needing to specify '.s'.
Comment #1 by john.michael.hall — 2020-06-04T19:38:12Z
When I compile below I get the error: "onlineapp.d(17): Error: no property x for type onlineapp.T"
struct S {
int x;
int y;
}
struct T {
S s;
alias s this;
}
static assert(is(typeof(T.init.x)));
static assert(is(typeof(T.init.y)));
struct U {
T t;
alias x = t.x; // <-- FAIL
alias y = t.s.x; // <-- OK
}
void main() {
}
Comment #2 by robert.schadek — 2024-12-13T18:22:40Z