getSymbolsByUDA

template getSymbolsByUDA (
alias symbol
alias attribute
) {}

Parameters

symbol

The aggregate type or module to search

attribute

The user-defined attribute to search for

Return Value

All symbols within symbol that have the given UDA attribute.

Note: This is not recursive; it will not search for symbols within symbols such as nested structs or unions.

Examples

enum Attr;
struct A
{
    @Attr int a;
    int b;
}

static assert(getSymbolsByUDA!(A, Attr).length == 1);
static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[0], Attr));
1 enum Attr;
2 
3 static struct A
4 {
5     @Attr int a;
6     int b;
7     @Attr void doStuff() {}
8     void doOtherStuff() {}
9     static struct Inner
10     {
11         // Not found by getSymbolsByUDA
12         @Attr int c;
13     }
14 }
15 
16 // Finds both variables and functions with the attribute, but
17 // doesn't include the variables and functions without it.
18 static assert(getSymbolsByUDA!(A, Attr).length == 2);
19 // Can access attributes on the symbols returned by getSymbolsByUDA.
20 static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[0], Attr));
21 static assert(hasUDA!(getSymbolsByUDA!(A, Attr)[1], Attr));

Finds multiple attributes

1 static struct UDA { string name; }
2 
3 static struct B
4 {
5     @UDA("X")
6     int x;
7     @UDA("Y")
8     int y;
9     @(100)
10     int z;
11 }
12 
13 // Finds both UDA attributes.
14 static assert(getSymbolsByUDA!(B, UDA).length == 2);
15 // Finds one `100` attribute.
16 static assert(getSymbolsByUDA!(B, 100).length == 1);
17 // Can get the value of the UDA from the return value
18 static assert(getUDAs!(getSymbolsByUDA!(B, UDA)[0], UDA)[0].name == "X");

Checks for UDAs on the aggregate symbol itself

static struct UDA { string name; }

@UDA("A")
static struct C
{
    @UDA("B")
    int d;
}

static assert(getSymbolsByUDA!(C, UDA).length == 2);
static assert(getSymbolsByUDA!(C, UDA)[0].stringof == "C");
static assert(getSymbolsByUDA!(C, UDA)[1].stringof == "d");

Finds nothing if there is no member with specific UDA

static struct UDA { string name; }

static struct D
{
    int x;
}

static assert(getSymbolsByUDA!(D, UDA).length == 0);

Meta

Suggestion Box / Bug Report