Making alias of a struct field needs "this".
Stanislav Blinov
stanislav.blinov at gmail.com
Tue Jun 2 13:37:25 UTC 2020
On Tuesday, 2 June 2020 at 09:28:01 UTC, realhet wrote:
> I did it that way:
> private enum fieldMap = [ // simple names for descriptive and
> structured fields
> "hauteur" : "general.height",
> "rayon" : "profile.radius",
> "plage" : "profile.plage",
> "offsetv" : "profile.verticalOffset",
> "offseth" : "profile.horizontalOffset",
> "varrad0" : "profile.linearVariationBottom",
> "varrad1" : "profile.linearVariationTop",
> "amplitude" : "periphery.amplitude",
> "varlin0" : "periphery.linearVariationBottom",
> "varlin1" : "periphery.linearVariationTop",
> "varsinp" : "periphery.cosinusVariationPlage",
> "varsino" : "periphery.cosinusVariationOffset",
> "periodes" : "periphery.nbPeriods",
> "revolution" : "periphery.turn"
> ];
>
> static foreach(k, v; fieldMap){ mixin("@property auto $()
> const{ return #; }".replace("$", k).replace("#", v)); } //I
> know, i know -> AliasSeq :D
>
> But it doesn't look nice.
Try UDAs instead of a map:
struct A {
struct G {
@("hauteur") int height;
}
struct P {
@("rayon") int radius;
@("plage") int plage;
@("offsetv") int verticalOffset;
@("offseth") int horizontalOffset;
int noShortcut;
@("varrad0") int linearVariationBottom;
}
G general;
P profile;
/* ... */
static foreach (i, m; typeof(this).tupleof)
static foreach (j, f; typeof(m).tupleof)
static if (__traits(getAttributes, f).length)
mixin("@property auto ", __traits(getAttributes,
f)[0], "() const { return this.tupleof[i].tupleof[j]; }");
}
pragma(msg, __traits(allMembers, A));
More information about the Digitalmars-d-learn
mailing list