On Friday, 28 February 2020 at 18:34:08 UTC, cc wrote:
This compiles:
class Foo {
int x;
@(1) void y() {}
this() {
static foreach (idx, field; getSymbolsByUDA!(Foo, 1)) {
}
}
}
This does not:
class Foo {
@(1) int x;
void y() {}
this() {
static foreach (idx, field; getSymbolsByUDA!(Foo, 1)) {
}
}
}
Error: value of `this` is not known at compile time
Is there an equivalent for getSymbolsByUDA for member
variables, or is this a bug?
I dont see a bug, the error message is correct.
I'd say that you have two options:
1. drop the `static` before `foreach`, for example to use the
symbols during run-time
2. to do metaprog adopt another style of loop, e.g
class Foo {
@(1) int x;
void y() {}
this() {
alias AtOne = getSymbolsByUDA!(Foo, 1);
static foreach (i; 0 .. AtOne.length) {
pragma(msg, __traits(identifier, AtOne[i]));
}
}
}