proper way to find if attribute present?

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 24 10:36:31 PDT 2013


On Thursday, October 24, 2013 18:22:43 Daniel Davidson wrote:
> enum Bar = "Bar";
> @("Foo") @Bar int x;
> pragma(msg, __traits(getAttributes, x));
> 
> This prints: tuple("Foo", "Bar")
> 
> How do you run code only if "Bar" is associated with a symbol
> like x?
> I was hoping something like this:
> pragma(msg, hasAnnotation!(x, Bar));

You should probably use std.traits.functionAttributes:

http://dlang.org/phobos/std_traits.html#.FunctionAttribute

And in general, you should favor using std.traits over __traits. Ideally, no 
code outside of the standard library would need to use __traits. That's not 
currently the case unfortunately, but it is the case that std.traits has much 
of what you need for anything involving static introspection.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list