digitalmars.D.learn - How do I check if this field is of this template struct?
- Jack (32/32) Mar 19 2021 give below template struct, how can I list the members x, y and
- Panke (12/17) Mar 19 2021 Template parameter cannot only be types but also values,
- Jack (3/21) Mar 19 2021 that design is a bit different but may be worth, gonna get used
- Paul Backus (3/8) Mar 19 2021 You want std.traits.isInstanceOf:
- Jack (34/43) Mar 19 2021 thanks this works fine outside a method but not in a static
- frame (2/6) Mar 19 2021
- Jack (3/9) Mar 19 2021 indeed, it shwos an example on how to use that in a static
- Steven Schveighoffer (4/49) Mar 19 2021 foo in this context is the *instantiated* foo (i.e. the struct foo
- Jack (4/57) Mar 19 2021 Thank you Steve, that what I was looking for. Funny thing, I
give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something. Here's the code: template foo(string s = null) { nogc struct foo { int n; enum x = foo!"str1"(10); enum y = foo!"str2"(20); enum z = foo!"str3"(30); enum myEnum { none } void someMethod() { } } } not working listener: void list() { static foreach(field; __traits(allMembers, foo!())) {{ alias m = __traits(getMember, foo!(), field); static if(!isType!m && __traits(isSame, OriginalType!(typeof(m)), foo)) { writefln("field = [%s]", field); } }} }
Mar 19 2021
On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.Template parameter cannot only be types but also values, including strings. If you instantiate a template with different values you get different types. -- struct foo(T) { } struct bar(string s) {} alias a = foo!string; // type of a is foo!string alias b = bar!"str1"; // type of b is bar!"str1" alias c = bar!"str2"; // typo of c is bar!"str2" static assert (!is(typeof(c) == typeof(b))); --
Mar 19 2021
On Friday, 19 March 2021 at 07:56:26 UTC, Panke wrote:On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:that design is a bit different but may be worth, gonna get used to itgive below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.Template parameter cannot only be types but also values, including strings. If you instantiate a template with different values you get different types.-- struct foo(T) { } struct bar(string s) {} alias a = foo!string; // type of a is foo!string alias b = bar!"str1"; // type of b is bar!"str1" alias c = bar!"str2"; // typo of c is bar!"str2" static assert (!is(typeof(c) == typeof(b))); --
Mar 19 2021
On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.You want std.traits.isInstanceOf: static if(!isType!m && isInstanceOf!(foo, typeof(m)))
Mar 19 2021
On Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:thanks this works fine outside a method but not in a static method. what am I missing? void main() { // doesn't print anything Foo.list(); } alias Foo = foo!(); template foo(string s = null) { nogc struct foo { int n; enum x = foo!"str1"(10); enum y = foo!"str2"(20); enum z = foo!"str3"(30); enum myEnum { none } void someMethod() { } static void list() { //writeln("members = ", [__traits(allMembers, foo!())]); static foreach(field; __traits(allMembers, foo!())) {{ alias m = __traits(getMember, foo!(), field); static if(!isType!m && isInstanceOf!(foo, typeof(m))) { writefln("field = [%s]", field); } }} } } }give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.You want std.traits.isInstanceOf: static if(!isType!m && isInstanceOf!(foo, typeof(m)))
Mar 19 2021
On Friday, 19 March 2021 at 16:41:11 UTC, Jack wrote:thanks this works fine outside a method but not in a static method. what am I missing?Reading the manual ;)To use isInstanceOf to check the identity of a template while inside of said template, use TemplateOf.
Mar 19 2021
On Friday, 19 March 2021 at 17:40:39 UTC, frame wrote:On Friday, 19 March 2021 at 16:41:11 UTC, Jack wrote:indeed, it shwos an example on how to use that in a static functionthanks this works fine outside a method but not in a static method. what am I missing?Reading the manual ;)To use isInstanceOf to check the identity of a template while inside of said template, use TemplateOf.
Mar 19 2021
On 3/19/21 12:41 PM, Jack wrote:On Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:foo in this context is the *instantiated* foo (i.e. the struct foo above). What you want is `isIsntanceOf!(.foo, typeof(m))` -SteveOn Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:thanks this works fine outside a method but not in a static method. what am I missing? void main() { // doesn't print anything Foo.list(); } alias Foo = foo!(); template foo(string s = null) { nogc struct foo { int n; enum x = foo!"str1"(10); enum y = foo!"str2"(20); enum z = foo!"str3"(30); enum myEnum { none } void someMethod() { } static void list() { //writeln("members = ", [__traits(allMembers, foo!())]); static foreach(field; __traits(allMembers, foo!())) {{ alias m = __traits(getMember, foo!(), field); static if(!isType!m && isInstanceOf!(foo, typeof(m)))give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.You want std.traits.isInstanceOf: static if(!isType!m && isInstanceOf!(foo, typeof(m)))
Mar 19 2021
On Saturday, 20 March 2021 at 00:16:06 UTC, Steven Schveighoffer wrote:On 3/19/21 12:41 PM, Jack wrote:Thank you Steve, that what I was looking for. Funny thing, I totally forget about the . operatorOn Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:foo in this context is the *instantiated* foo (i.e. the struct foo above). What you want is `isIsntanceOf!(.foo, typeof(m))` -SteveOn Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:thanks this works fine outside a method but not in a static method. what am I missing? void main() { // doesn't print anything Foo.list(); } alias Foo = foo!(); template foo(string s = null) { nogc struct foo { int n; enum x = foo!"str1"(10); enum y = foo!"str2"(20); enum z = foo!"str3"(30); enum myEnum { none } void someMethod() { } static void list() { //writeln("members = ", [__traits(allMembers, foo!())]); static foreach(field; __traits(allMembers, foo!())) {{ alias m = __traits(getMember, foo!(), field); static if(!isType!m && isInstanceOf!(foo, typeof(m)))give below template struct, how can I list the members x, y and z? I've tried something with OriginalType and TemplateOf but no luck... it seems if I do foo!"str1" the "str1" became "part of type"? give .stringof from typeof(__traits(getMember, foo, field)) I thought the type would be foo!string or something.You want std.traits.isInstanceOf: static if(!isType!m && isInstanceOf!(foo, typeof(m)))
Mar 19 2021