Avoiding __traits(getAttributes, ...) on alias

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 9 05:19:10 PDT 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 Frijters

You could always do a static if with __traits(compiles, 
__traits(getAttributes, mixin(e))


More information about the Digitalmars-d-learn mailing list