How can I get this UDA at compile time?

Jack jckj33 at gmail.com
Sun Feb 21 06:12:50 UTC 2021


I've had a struct like this:

struct Attr
{
	string value;
}

struct Foo
{
	@(Attr("a attr"))
	enum a = Foo(10);

	@(Attr("b attr"))
	enum b = Foo(11);


	int x;
	int y;
	bool doY = true;
	
	int value()
	{

		return x;
	}

}

I'd like to get that atrribute's value at compile time, something 
like this:

	enum s = Foo.a.baa;
	enum s2 = Foo.b.baa;
	writeln(s); // a attr
	writeln(s2); // b attr

I did this:

	string baa()
	{
		import std.traits : getUDAs, hasUDA;

		static foreach(field; __traits(allMembers, Foo))
		{{
			alias m = __traits(getMember, Foo, field);
			static if(is(typeof(m) == Foo))
			{
				if(m.value == this.value)
					return getUDAs!(m, Attr)[0].value;
			}
		}}

		return null;
	}

that was working fine, but I needed to switch value property from 
Foo struct, so that I can't read this value at CTFE anymore, so 
this fails now:

			if(m.value == this.value)
					return getUDAs!(m, Attr)[0].value;

How can I solve this?


More information about the Digitalmars-d-learn mailing list