On Sunday, 31 May 2020 at 00:46:09 UTC, Paul Backus wrote:
You can simplify this considerably using a mixin template [1]:
---
mixin template RW(T, string name) {
private T var;
public T get() { return var; }
public typeof(this) set(T val) { var = val; return this; }
mixin("private alias _", name, " = var;");
// two aliases with the same name create an overload set
mixin("public alias ", name, " = get;");
mixin("public alias ", name, " = set;");
}
class Point {
mixin RW!(int, "x");
mixin RW!(int, "y");
mixin RW!(string, "z"); // add
}
---
This is better, ... but it breaks std.traits:
void main() {
auto fields = FieldNameTuple!(Point);
writeln(fields);
}
$ ./b
varvarvar
And normally, we cannot define 2 fields with different types:
class P {
int x;
double x; // b.d(45): Error: variable b.P.x conflicts with
variable b.P.x at b.d(44)
}
With the above template we somehow tricked the compiler to be
able to do this?
Is this a loop-hole we should file a bug?