digitalmars.D.learn - Avoiding __traits(getAttributes, ...) on alias
- Stefan Frijters (27/27) May 09 2014 I've been playing with UDAs a bit and I wanted to find all
- John Colvin (3/30) May 09 2014 You could always do a static if with __traits(compiles,
- Stefan Frijters (5/41) May 09 2014 Thank you for the fast reply; this solves my problem. I actually
- Vlad Levenfeld (17/17) May 09 2014 I've recently found out that you can deal with the abundance of
- Philippe Sigaud via Digitalmars-d-learn (2/9) May 09 2014 What are the differences?
- Vlad Levenfeld (16/16) May 10 2014 I'm not really sure. Here is an example of the problem:
- Vlad Levenfeld (4/4) May 10 2014 because this works:
- Vlad Levenfeld (3/3) May 10 2014 I don't have any examples of the const bool thing not working,
- Philippe Sigaud via Digitalmars-d-learn (3/9) May 10 2014 Maybe __traits is trying to extract attributes from the mixin
- Vlad Levenfeld (1/1) May 10 2014 Agreed, new bug report submitted.
I've been playing with UDAs a bit and I wanted to find all variables with a particular attribute in various modules. I thought I had it cracked, until I added a module that contains an alias declaration, which makes it choke when trying to execute __traits(getAttributes, ...). A small example is shown below. Is there any conditional I can insert between the two foreach lines to make it detect such an alias declaration, and move on to the next derived member? Or should getAttributes handle this by just returning no attributes? import std.traits; ("testattr") int foo; alias char[256] MyChar; ("testattr") int bar; void main() { foreach(e ; __traits(derivedMembers, mixin(__MODULE__))) { foreach( t; __traits(getAttributes, mixin(e)) ){ pragma(msg, t); } } // testattr // test.d(9): Error: first argument is not a symbol // test.d(9): Error: invalid foreach aggregate false // testattr } Any hints would be appreciated! Kind regards, Stefan Frijters
May 09 2014
On Friday, 9 May 2014 at 11:53:59 UTC, Stefan Frijters wrote:I've been playing with UDAs a bit and I wanted to find all variables with a particular attribute in various modules. I thought I had it cracked, until I added a module that contains an alias declaration, which makes it choke when trying to execute __traits(getAttributes, ...). A small example is shown below. Is there any conditional I can insert between the two foreach lines to make it detect such an alias declaration, and move on to the next derived member? Or should getAttributes handle this by just returning no attributes? import std.traits; ("testattr") int foo; alias char[256] MyChar; ("testattr") int bar; void main() { foreach(e ; __traits(derivedMembers, mixin(__MODULE__))) { foreach( t; __traits(getAttributes, mixin(e)) ){ pragma(msg, t); } } // testattr // test.d(9): Error: first argument is not a symbol // test.d(9): Error: invalid foreach aggregate false // testattr } Any hints would be appreciated! Kind regards, Stefan FrijtersYou could always do a static if with __traits(compiles, __traits(getAttributes, mixin(e))
May 09 2014
On Friday, 9 May 2014 at 12:19:12 UTC, John Colvin wrote:On Friday, 9 May 2014 at 11:53:59 UTC, Stefan Frijters wrote:Thank you for the fast reply; this solves my problem. I actually tried this before, but in my actual code instead of the example, where I'm deep into backticks and quotes and escaped quotes so I probably made a mistake there...I've been playing with UDAs a bit and I wanted to find all variables with a particular attribute in various modules. I thought I had it cracked, until I added a module that contains an alias declaration, which makes it choke when trying to execute __traits(getAttributes, ...). A small example is shown below. Is there any conditional I can insert between the two foreach lines to make it detect such an alias declaration, and move on to the next derived member? Or should getAttributes handle this by just returning no attributes? import std.traits; ("testattr") int foo; alias char[256] MyChar; ("testattr") int bar; void main() { foreach(e ; __traits(derivedMembers, mixin(__MODULE__))) { foreach( t; __traits(getAttributes, mixin(e)) ){ pragma(msg, t); } } // testattr // test.d(9): Error: first argument is not a symbol // test.d(9): Error: invalid foreach aggregate false // testattr } Any hints would be appreciated! Kind regards, Stefan FrijtersYou could always do a static if with __traits(compiles, __traits(getAttributes, mixin(e))
May 09 2014
I've recently found out that you can deal with the abundance of backticks and escapes in a couple of ways: q{ your code here... } will resolve to a string but your editor can still highlight it as D code, making it more readable. also, turning things like: mixin ("if ("~expr~")") into: if (mixin(expr)) can improve readability but beware I've noticed that sometimes this is not equivalent to the previous version and I'm not sure how or why that happens. In particular I notice that mixin("const bool value = "~expr~";)"); and const bool value = mixin(expr); are not the same, for some reason.
May 09 2014
Vlad Levenfeld:but beware I've noticed that sometimes this is not equivalent to the previous version and I'm not sure how or why that happens. In particular I notice that mixin("const bool value = "~expr~";)"); and const bool value = mixin(expr); are not the same, for some reason.What are the differences?
May 09 2014
I'm not really sure. Here is an example of the problem: const bool has_attribute (T, string member, alias attribute) () { static if (1) // ok mixin( `foreach (type; __traits (getAttributes, T.`~member~`))` `static if (is (type == attribute))` `return true;` ); else // doesn't compile, member is not accessible error foreach (type; __traits (getAttributes, mixin(`T.`~member))) static if (is (type == attribute)) return true; return false; } Maybe its trying to use it inside of __traits that is causing it?
May 10 2014
because this works: foreach (type; mixin(`__traits (getAttributes, T.`~member~`)`)) static if (is (type == attribute)) return true;
May 10 2014
I don't have any examples of the const bool thing not working, its just something I feel like I recall, though I could be mistaking it for the __traits example.
May 10 2014
else // doesn't compile, member is not accessible error foreach (type; __traits (getAttributes, mixin(`T.`~member))) static if (is (type == attribute)) return true; return false;Maybe its trying to use it inside of __traits that is causing it?Maybe __traits is trying to extract attributes from the mixin expression, before the mixin injection? That looks like a bug to me, no?
May 10 2014