proper way to find if attribute present?

Daniel Davidson nospam at spam.com
Thu Oct 24 09:22:43 PDT 2013


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));

Where getAnnotation from 
(http://forum.dlang.org/thread/jxbdiwyyxwnmmybiqvyq@forum.dlang.org)
was written as:

template hasAnnotation(alias f, Attr) {
   bool helper() {
     foreach(attr; __traits(getAttributes, f))
       static if(is(attr == Attr) || is(typeof(attr) == Attr))
         return true;
     return false;

   }
   enum bool hasAnnotation = helper;
}


I am missing how this can be used.

Thanks
Dan


More information about the Digitalmars-d-learn mailing list