Struct fields and properties as alias members

Adam D Ruppe destructionator at gmail.com
Wed Dec 8 18:23:25 UTC 2021


On Wednesday, 8 December 2021 at 18:07:32 UTC, Ben Jones wrote:
> Since I reuse the field a few times I tried to alias the result:
>
> `alias theProp = __traits(child, s, field);`

yeah won't work since the alias again drops the `this`, this is 
the same thing as the template param.

You could make a couple helper functions though. Like

void set(typeof(field) v) {
    __traits(child, s, field) = v;
}

typeof(field) get() {
    return __traits(child, s, field);
}

just declare them nested inside the function and then you can use 
get and set as-needed. You could make a mixin or whatever if 
there's a bunch of functions doing this.



Of course another idea is to make a helper struct that just wraps 
the field with opAssign and alias this or something but then it 
needs to keep a pointer back to s inside that object.


More information about the Digitalmars-d-learn mailing list