1 struct Attr 2 { 3 string name; 4 int value; 5 } 6 7 @Attr("Answer", 42) int a; 8 static assert(getUDAs!(a, Attr).length == 1); 9 static assert(getUDAs!(a, Attr)[0].name == "Answer"); 10 static assert(getUDAs!(a, Attr)[0].value == 42); 11 12 @(Attr("Answer", 42), "string", 9999) int b; 13 static assert(getUDAs!(b, Attr).length == 1); 14 static assert(getUDAs!(b, Attr)[0].name == "Answer"); 15 static assert(getUDAs!(b, Attr)[0].value == 42); 16 17 @Attr("Answer", 42) @Attr("Pi", 3) int c; 18 static assert(getUDAs!(c, Attr).length == 2); 19 static assert(getUDAs!(c, Attr)[0].name == "Answer"); 20 static assert(getUDAs!(c, Attr)[0].value == 42); 21 static assert(getUDAs!(c, Attr)[1].name == "Pi"); 22 static assert(getUDAs!(c, Attr)[1].value == 3); 23 24 static assert(getUDAs!(c, Attr("Answer", 42)).length == 1); 25 static assert(getUDAs!(c, Attr("Answer", 42))[0].name == "Answer"); 26 static assert(getUDAs!(c, Attr("Answer", 42))[0].value == 42); 27 28 static assert(getUDAs!(c, Attr("Answer", 99)).length == 0); 29 30 struct AttrT(T) 31 { 32 string name; 33 T value; 34 } 35 36 @AttrT!uint("Answer", 42) @AttrT!int("Pi", 3) @AttrT int d; 37 static assert(getUDAs!(d, AttrT).length == 2); 38 static assert(getUDAs!(d, AttrT)[0].name == "Answer"); 39 static assert(getUDAs!(d, AttrT)[0].value == 42); 40 static assert(getUDAs!(d, AttrT)[1].name == "Pi"); 41 static assert(getUDAs!(d, AttrT)[1].value == 3); 42 43 static assert(getUDAs!(d, AttrT!uint).length == 1); 44 static assert(getUDAs!(d, AttrT!uint)[0].name == "Answer"); 45 static assert(getUDAs!(d, AttrT!uint)[0].value == 42); 46 47 static assert(getUDAs!(d, AttrT!int).length == 1); 48 static assert(getUDAs!(d, AttrT!int)[0].name == "Pi"); 49 static assert(getUDAs!(d, AttrT!int)[0].value == 3); 50 51 struct SimpleAttr {} 52 53 @SimpleAttr int e; 54 static assert(getUDAs!(e, SimpleAttr).length == 1); 55 static assert(is(getUDAs!(e, SimpleAttr)[0] == SimpleAttr)); 56 57 @SimpleAttr() int f; 58 static assert(getUDAs!(f, SimpleAttr).length == 1); 59 static assert(is(typeof(getUDAs!(f, SimpleAttr)[0]) == SimpleAttr)); 60 61 struct FuncAttr(alias f) { alias func = f; } 62 static int add42(int v) { return v + 42; } 63 static string concat(string l, string r) { return l ~ r; } 64 65 @FuncAttr!add42 int g; 66 static assert(getUDAs!(g, FuncAttr).length == 1); 67 static assert(getUDAs!(g, FuncAttr)[0].func(5) == 47); 68 69 static assert(getUDAs!(g, FuncAttr!add42).length == 1); 70 static assert(getUDAs!(g, FuncAttr!add42)[0].func(5) == 47); 71 72 static assert(getUDAs!(g, FuncAttr!add42()).length == 0); 73 74 static assert(getUDAs!(g, FuncAttr!concat).length == 0); 75 static assert(getUDAs!(g, FuncAttr!concat()).length == 0); 76 77 @FuncAttr!add42() int h; 78 static assert(getUDAs!(h, FuncAttr).length == 1); 79 static assert(getUDAs!(h, FuncAttr)[0].func(5) == 47); 80 81 static assert(getUDAs!(h, FuncAttr!add42).length == 1); 82 static assert(getUDAs!(h, FuncAttr!add42)[0].func(5) == 47); 83 84 static assert(getUDAs!(h, FuncAttr!add42()).length == 1); 85 static assert(getUDAs!(h, FuncAttr!add42())[0].func(5) == 47); 86 87 static assert(getUDAs!(h, FuncAttr!concat).length == 0); 88 static assert(getUDAs!(h, FuncAttr!concat()).length == 0); 89 90 @("alpha") @(42) int i; 91 static assert(getUDAs!(i, "alpha").length == 1); 92 static assert(getUDAs!(i, "alpha")[0] == "alpha"); 93 94 static assert(getUDAs!(i, 42).length == 1); 95 static assert(getUDAs!(i, 42)[0] == 42); 96 97 static assert(getUDAs!(i, 'c').length == 0);
Gets the matching user-defined attributes from the given symbol.
If the UDA is a type, then any UDAs of the same type on the symbol will match. If the UDA is a template for a type, then any UDA which is an instantiation of that template will match. And if the UDA is a value, then any UDAs on the symbol which are equal to that value will match.