digitalmars.D.learn - Metaprogramming with traits
- Ram_B (18/18) Sep 15 2016 How i can get fields of derived classes in runtime? This not works
- rikki cattermole (4/22) Sep 15 2016 A few weeks ago I wrote an article for TWID that covers this very subjec...
- Gary Willoughby (26/28) Sep 15 2016 What about something like this:
- Ram_B (3/31) Sep 15 2016 Thanks very much! :3
- Ram_B (36/64) Sep 15 2016 This works, but i can't use (get|has)Member
- Gary Willoughby (2/7) Sep 16 2016 Maybe Array!(string) can't be use in compile-time code?
- Ram_B (3/10) Sep 16 2016 Which type of array i may use to pass it to compile-time function?
How i can get fields of derived classes in runtime? This not works import std.traits; import std.experimental.logger; class A { int a,b; this(){} void fields(){ log(FieldNameTuple!this); } } class B : A{ int c; this(){} } void main(){ B b = new B(); b.fields(); }
Sep 15 2016
On 16/09/2016 3:07 AM, Ram_B wrote:How i can get fields of derived classes in runtime? This not works import std.traits; import std.experimental.logger; class A { int a,b; this(){} void fields(){ log(FieldNameTuple!this); } } class B : A{ int c; this(){} } void main(){ B b = new B(); b.fields(); }A few weeks ago I wrote an article for TWID that covers this very subject. Sadly because of a bug in dmd it doesn't work for all cases perfectly. http://arsdnet.net/this-week-in-d/2016-aug-28.html
Sep 15 2016
On Thursday, 15 September 2016 at 15:07:09 UTC, Ram_B wrote:How i can get fields of derived classes in runtime? This not worksWhat about something like this: import std.traits; import std.stdio; class A { int a,b; this(){} void fields(this T)(){ writeln(FieldNameTuple!(T)); foreach(Class; TransitiveBaseTypeTuple!(T)) { writeln(FieldNameTuple!(Class)); } } } class B : A{ int c; this(){} } class C : B{ int d; this(){} } void main(){ C c = new C(); c.fields(); }
Sep 15 2016
On Thursday, 15 September 2016 at 15:56:56 UTC, Gary Willoughby wrote:On Thursday, 15 September 2016 at 15:07:09 UTC, Ram_B wrote:Thanks very much! :3How i can get fields of derived classes in runtime? This not worksWhat about something like this: import std.traits; import std.stdio; class A { int a,b; this(){} void fields(this T)(){ writeln(FieldNameTuple!(T)); foreach(Class; TransitiveBaseTypeTuple!(T)) { writeln(FieldNameTuple!(Class)); } } } class B : A{ int c; this(){} } class C : B{ int d; this(){} } void main(){ C c = new C(); c.fields(); }
Sep 15 2016
On Thursday, 15 September 2016 at 15:56:56 UTC, Gary Willoughby wrote:On Thursday, 15 September 2016 at 15:07:09 UTC, Ram_B wrote:This works, but i can't use (get|has)Member class A{ int a, b; Array!string fields(this T)(){ Array!string list; foreach(field; FieldNameTuple!(T)){ list.insertBack(field); } foreach(Class; TransitiveBaseTypeTuple!(T)){ foreach(field; FieldNameTuple!(Class)){ list.insertBack(field); } } return list; } void t(this T)(){ foreach(f; this.fields()){ log(__traits(hasMember, T, f)); } } } class B : A{ int c; this(){} } void main(){ B b = new B(); b.t(); } test.d(33): Error: variable f cannot be read at compile time test.d(33): Error: string expected as second argument of __traits hasMember instead of __error test.d(46): Error: template instance test.A.t!(B) error instantiatingHow i can get fields of derived classes in runtime? This not worksWhat about something like this: import std.traits; import std.stdio; class A { int a,b; this(){} void fields(this T)(){ writeln(FieldNameTuple!(T)); foreach(Class; TransitiveBaseTypeTuple!(T)) { writeln(FieldNameTuple!(Class)); } } } class B : A{ int c; this(){} } class C : B{ int d; this(){} } void main(){ C c = new C(); c.fields(); }
Sep 15 2016
On Thursday, 15 September 2016 at 22:05:55 UTC, Ram_B wrote:test.d(33): Error: variable f cannot be read at compile time test.d(33): Error: string expected as second argument of __traits hasMember instead of __error test.d(46): Error: template instance test.A.t!(B) error instantiatingMaybe Array!(string) can't be use in compile-time code?
Sep 16 2016
On Friday, 16 September 2016 at 08:01:18 UTC, Gary Willoughby wrote:On Thursday, 15 September 2016 at 22:05:55 UTC, Ram_B wrote:Which type of array i may use to pass it to compile-time function?test.d(33): Error: variable f cannot be read at compile time test.d(33): Error: string expected as second argument of __traits hasMember instead of __error test.d(46): Error: template instance test.A.t!(B) error instantiatingMaybe Array!(string) can't be use in compile-time code?
Sep 16 2016