Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 19, 2020 Getting FieldNameTuple including all base-classes. | ||||
---|---|---|---|---|
| ||||
Hi, I was able to reach all the fields in a subclass using foreach and BaseClassesTuple, but is there a way to do this using functional programming primitives, but in compile time for tuples? private template FieldNameTuple2(T) { enum FieldNameTuple2 = BaseClassesTuple!T.map!(S => FieldNameTuple!S).join; } This is obviously wrong, but is there a way to do it? I've searched for FieldNameTuple and BaseClassesTuple, but without luck. It's weird because it seems like a common thing to do. Thanks in advance! |
May 20, 2020 Re: Getting FieldNameTuple including all base-classes. | ||||
---|---|---|---|---|
| ||||
Posted in reply to realhet | On Tuesday, 19 May 2020 at 23:15:45 UTC, realhet wrote:
> Hi,
>
> I was able to reach all the fields in a subclass using foreach and BaseClassesTuple, but is there a way to do this using functional programming primitives, but in compile time for tuples?
>
> private template FieldNameTuple2(T) {
> enum FieldNameTuple2 = BaseClassesTuple!T.map!(S => FieldNameTuple!S).join;
> }
>
> This is obviously wrong, but is there a way to do it?
>
> I've searched for FieldNameTuple and BaseClassesTuple, but without luck. It's weird because it seems like a common thing to do.
>
> Thanks in advance!
I think what you want is `std.meta.staticMap`. Something like this:
alias FieldNameTuple2(T) = staticMap!(FieldNameTuple, BaseClassesTuple!T);
|
May 20, 2020 Re: Getting FieldNameTuple including all base-classes. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Wednesday, 20 May 2020 at 01:18:24 UTC, Paul Backus wrote: > On Tuesday, 19 May 2020 at 23:15:45 UTC, realhet wrote: > I think what you want is `std.meta.staticMap`. Something like this: > > alias FieldNameTuple2(T) = staticMap!(FieldNameTuple, BaseClassesTuple!T); class A { int a, a1; } class B : A { int b; } class C : B { int c, c1; } alias AllClasses(T) = Reverse!(AliasSeq!(T, BaseClassesTuple!T[0..$-1])); alias AllFieldNames(T) = staticMap!(FieldNameTuple, AllClasses!T); void main(){ static foreach(T; "ABC") mixin("AllFieldNames!"~T~".stringof.writeln;"); } That statticMap unlike the normal map, it also joins the AliasSeq at the end. Just what I needed. Thank You! |
May 20, 2020 Re: Getting FieldNameTuple including all base-classes. | ||||
---|---|---|---|---|
| ||||
Posted in reply to realhet | On Wednesday, 20 May 2020 at 09:03:51 UTC, realhet wrote:
> On Wednesday, 20 May 2020 at 01:18:24 UTC, Paul Backus wrote:
>> On Tuesday, 19 May 2020 at 23:15:45 UTC, realhet wrote:
With this mod it also works with structs:
template AllClasses(T){
static if(is(T == class))
alias AllClasses = Reverse!(AliasSeq!(T, BaseClassesTuple!T[0..$-1]));
else
alias AllClasses = T;
}
|
May 20, 2020 Re: Getting FieldNameTuple including all base-classes. | ||||
---|---|---|---|---|
| ||||
Posted in reply to realhet | On 5/20/20 12:03 PM, realhet wrote: > On Wednesday, 20 May 2020 at 01:18:24 UTC, Paul Backus wrote: >> On Tuesday, 19 May 2020 at 23:15:45 UTC, realhet wrote: >> I think what you want is `std.meta.staticMap`. Something like this: >> >> alias FieldNameTuple2(T) = staticMap!(FieldNameTuple, BaseClassesTuple!T); > > class A { int a, a1; } > class B : A { int b; } > class C : B { int c, c1; } > > alias AllClasses(T) = Reverse!(AliasSeq!(T, BaseClassesTuple!T[0..$-1])); > alias AllFieldNames(T) = staticMap!(FieldNameTuple, AllClasses!T); > > void main(){ > static foreach(T; "ABC") mixin("AllFieldNames!"~T~".stringof.writeln;"); > } > > That statticMap unlike the normal map, it also joins the AliasSeq at the end. Just what I needed. > > Thank You! Just in case - you can avoid mixin using: ```D static foreach(T; AliasSeq!(A, B, C)) AllFieldNames!T.stringof.writeln; ``` https://run.dlang.io/is/Q6omgL |
Copyright © 1999-2021 by the D Language Foundation