www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Issue with traits usability

reply =?UTF-8?B?TcOhcmNpbw==?= Martins <marcioapm gmail.com> writes:
Hi!

How do I get past this?


static struct X {
	int x;
	private enum T = 1;
	private alias M = string;
}

foreach (Member; __traits(allMembers, X)) {
	pragma(msg, __traits(getProtection, __traits(getMember, X, 
Member)));
}


Output:

public
private
c.d(1084): Error: argument string has no protection
c.d(1084):        while evaluating pragma(msg, 
__traits(getProtection, string))


What I want to achieve essentially is skip all X's aliases and 
templates, but get everything else, like enums and data members. 
How would I go about this in a robust way?
I actually want to skip aliases, and templates.
Mar 21 2018
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Wednesday, 21 March 2018 at 15:36:01 UTC, Márcio Martins wrote:
 Hi!

 How do I get past this?


 static struct X {
 	int x;
 	private enum T = 1;
 	private alias M = string;
 }

 foreach (Member; __traits(allMembers, X)) {
 	pragma(msg, __traits(getProtection, __traits(getMember, X, 
 Member)));
 }


 Output:

 public
 private
 c.d(1084): Error: argument string has no protection
 c.d(1084):        while evaluating pragma(msg, 
 __traits(getProtection, string))


 What I want to achieve essentially is skip all X's aliases and 
 templates, but get everything else, like enums and data 
 members. How would I go about this in a robust way?
 I actually want to skip aliases, and templates.
__traits(compiles) is your friend: unittest { static struct X { int x; private enum T = 1; private alias M = string; } foreach (Member; __traits(allMembers, X)) { static if (__traits(compiles, __traits(getProtection, __traits(getMember, X, Member)))) { pragma(msg, __traits(getProtection, __traits(getMember, X, Member))); } } } -- Simen
Mar 22 2018
prev sibling parent Meta <jared771 gmail.com> writes:
On Wednesday, 21 March 2018 at 15:36:01 UTC, Márcio Martins wrote:
 Hi!

 How do I get past this?


 static struct X {
 	int x;
 	private enum T = 1;
 	private alias M = string;
 }

 foreach (Member; __traits(allMembers, X)) {
 	pragma(msg, __traits(getProtection, __traits(getMember, X, 
 Member)));
 }


 Output:

 public
 private
 c.d(1084): Error: argument string has no protection
 c.d(1084):        while evaluating pragma(msg, 
 __traits(getProtection, string))


 What I want to achieve essentially is skip all X's aliases and 
 templates, but get everything else, like enums and data 
 members. How would I go about this in a robust way?
 I actually want to skip aliases, and templates.
In addition to Simen's answer, you can you std.traits.FieldNameTuple to get only the fields of your struct (which only includes struct members that are actually part of the struct's memory layout): import std.traits; static struct X { int x; private enum T = 1; private alias M = string; } void main() { foreach (Member; FieldNameTuple!X) { pragma(msg, __traits(getProtection, __traits(getMember, X, Member))); } } This example prints "public" once.
Mar 23 2018