proper way to find if attribute present?
Dicebot
public at dicebot.lv
Thu Oct 24 10:54:24 PDT 2013
I use this small helper in vibe.d:
=======================
template extractUda(UDA, alias Symbol)
{
import std.typetuple : TypeTuple;
private alias TypeTuple!(__traits(getAttributes, Symbol))
udaTuple;
private template extract(list...)
{
static if (!list.length)
enum extract = null;
else {
static assert (!is(list[0] == UDA), "extractUda is
designed to look up values, not types");
static if (is(typeof(list[0]) == UDA))
enum extract = list[0];
else
enum extract = extract!(list[1..$]);
}
}
enum extractUda = extract!udaTuple;
}
=======================
Used like this:
=======================
@Bar("value") void foo();
enum uda = extractUda!(Bar, foo);
static if (is(typeof(uda) != typeof(null)))
static assert (uda == Bar("value"));
=======================
It is far from perfection but can serve as a starting point for
more complete implementation (though I have kept certain
limitations intentionally)
More information about the Digitalmars-d-learn
mailing list