On Thursday, 24 October 2013 at 16:22:44 UTC, Daniel Davidson wrote:
template hasAnnotation(alias f, Attr) {

This function looks for annotations as identified by type. Instead of using a plain string, you should make them some kind of struct:

struct MyAnnotation {
   string value;
}

Then you attach it like this:

@MyAnnotation("some value") void foo() {}

Then you'll be able to get it with the other two functions in there:

static if(hasValueAnnotation!(foo, MyAnnotation)) {
pragma(msg, getAnnotation!(foo, MyAnnotation)); // prints MyAnnotation("some value")

    // or you can fetch it into a variable:
    MyAnnotation a = getAnnotation!(foo, MyAnnotation);
    assert(a.value == "some value");
}


It is possible to use plain string in place of the MyAnnotation struct:

 @("some value") void foo() {}

static if(hasValueAnnotation!(foo, string)) {
pragma(msg, getAnnotation!(foo, string)); // prints MyAnnotation("some value")

    // or you can fetch it into a variable:
    string a = getAnnotation!(foo, string);
    assert(a == "some value");
}


But that isn't as reliable across modules because strings might be reused by anyone. A struct would always have a unique identifier - the struct name, which can be disambiguated by module.

Reply via email to