Showing with 19 additions and 2 deletions.
  1. +19 −2 std/traits.d
21 changes: 19 additions & 2 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -6956,9 +6956,12 @@ template getSymbolsByUDA(alias symbol, alias attribute) {
.format(names[0]));
}

enum hasSpecificUDA(string name) = mixin("hasUDA!(symbol.%s, attribute)".format(name));
// filtering out nested class context
enum noThisMember(string name) = (name != "this");
alias membersWithoutNestedCC = Filter!(noThisMember, __traits(allMembers, symbol));

alias membersWithUDA = toSymbols!(Filter!(hasSpecificUDA, __traits(allMembers, symbol)));
enum hasSpecificUDA(string name) = mixin("hasUDA!(symbol.%s, attribute)".format(name));
alias membersWithUDA = toSymbols!(Filter!(hasSpecificUDA, membersWithoutNestedCC));

// if the symbol itself has the UDA, tack it on to the front of the list
static if (hasUDA!(symbol, attribute))
Expand Down Expand Up @@ -7042,6 +7045,20 @@ template getSymbolsByUDA(alias symbol, alias attribute) {
static assert(hasUDA!(getSymbolsByUDA!(HasPrivateMembers, Attr)[1], Attr));
}

// #16387: getSymbolsByUDA works with structs but fails with classes
unittest
{
enum Attr;
class A
{
@Attr uint a;
}

alias res = getSymbolsByUDA!(A, Attr);
static assert(res.length == 1);
static assert(res[0].stringof == "a");
}

/**
Returns: $(D true) iff all types $(D T) are the same.
*/
Expand Down