www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting FieldNameTuple including all base-classes.

reply realhet <real_het hotmail.com> writes:
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 19 2020
parent reply Paul Backus <snarwin gmail.com> writes:
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 19 2020
parent reply realhet <real_het hotmail.com> writes:
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
next sibling parent realhet <real_het hotmail.com> writes:
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
prev sibling parent drug <drug2004 bk.ru> writes:
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
May 20 2020