hasUDA

Determine if a symbol has a given user-defined attribute.

enum hasUDA (
alias symbol
alias attribute
)

Examples

1 enum E;
2 struct S {}
3 
4 @("alpha") int a;
5 static assert(hasUDA!(a, "alpha"));
6 static assert(!hasUDA!(a, S));
7 static assert(!hasUDA!(a, E));
8 
9 @(E) int b;
10 static assert(!hasUDA!(b, "alpha"));
11 static assert(!hasUDA!(b, S));
12 static assert(hasUDA!(b, E));
13 
14 @E int c;
15 static assert(!hasUDA!(c, "alpha"));
16 static assert(!hasUDA!(c, S));
17 static assert(hasUDA!(c, E));
18 
19 @(S, E) int d;
20 static assert(!hasUDA!(d, "alpha"));
21 static assert(hasUDA!(d, S));
22 static assert(hasUDA!(d, E));
23 
24 @S int e;
25 static assert(!hasUDA!(e, "alpha"));
26 static assert(hasUDA!(e, S));
27 static assert(!hasUDA!(e, S()));
28 static assert(!hasUDA!(e, E));
29 
30 @S() int f;
31 static assert(!hasUDA!(f, "alpha"));
32 static assert(hasUDA!(f, S));
33 static assert(hasUDA!(f, S()));
34 static assert(!hasUDA!(f, E));
35 
36 @(S, E, "alpha") int g;
37 static assert(hasUDA!(g, "alpha"));
38 static assert(hasUDA!(g, S));
39 static assert(hasUDA!(g, E));
40 
41 @(100) int h;
42 static assert(hasUDA!(h, 100));
43 
44 struct Named { string name; }
45 
46 @Named("abc") int i;
47 static assert(hasUDA!(i, Named));
48 static assert(hasUDA!(i, Named("abc")));
49 static assert(!hasUDA!(i, Named("def")));
50 
51 struct AttrT(T)
52 {
53     string name;
54     T value;
55 }
56 
57 @AttrT!int("answer", 42) int j;
58 static assert(hasUDA!(j, AttrT));
59 static assert(hasUDA!(j, AttrT!int));
60 static assert(!hasUDA!(j, AttrT!string));
61 
62 @AttrT!string("hello", "world") int k;
63 static assert(hasUDA!(k, AttrT));
64 static assert(!hasUDA!(k, AttrT!int));
65 static assert(hasUDA!(k, AttrT!string));
66 
67 struct FuncAttr(alias f) { alias func = f; }
68 static int fourtyTwo() { return 42; }
69 static size_t getLen(string s) { return s.length; }
70 
71 @FuncAttr!getLen int l;
72 static assert(hasUDA!(l, FuncAttr));
73 static assert(!hasUDA!(l, FuncAttr!fourtyTwo));
74 static assert(hasUDA!(l, FuncAttr!getLen));
75 static assert(!hasUDA!(l, FuncAttr!fourtyTwo()));
76 static assert(!hasUDA!(l, FuncAttr!getLen()));
77 
78 @FuncAttr!getLen() int m;
79 static assert(hasUDA!(m, FuncAttr));
80 static assert(!hasUDA!(m, FuncAttr!fourtyTwo));
81 static assert(hasUDA!(m, FuncAttr!getLen));
82 static assert(!hasUDA!(m, FuncAttr!fourtyTwo()));
83 static assert(hasUDA!(m, FuncAttr!getLen()));

See Also

Meta