Generating code based on UDA

Rares Pop via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 25 07:45:38 PDT 2014


On Saturday, 25 October 2014 at 13:53:35 UTC, Adam D. Ruppe wrote:
> On Saturday, 25 October 2014 at 13:45:29 UTC, Rares Pop wrote:
>> What do you mean by copy/pasting ?
>
> I literally copied the code in your post (and fixed a missing 
> semicolon) and got it to compile.
>
> Passing A as an argument to injections should work. You can 
> also use this and typeof(this) inside the injections template 
> code to access the class. It should all work.

Taking this one step further, it looks like the attributes are 
not available at the mixin scope.
Here is my code:

------

struct Inject {
//	immutable Scope scoped;
}

static string injections(T)()
{
	pragma(msg, "injections for : ", T);
	string result;
	foreach(member; __traits(allMembers,T))
	{		
		enum fullName = format("%s.%s", T.stringof, member);
		pragma(msg, "member: ", fullName);
		auto attributes = __traits(getAttributes, fullName);
		enum dbg_msg = format ("%s attributes are %s", fullName, 
attributes.stringof);
		pragma(msg, dbg_msg);
		foreach(attr;attributes){
			pragma(msg, "Checking attribute", attr);
		}

	}
	return result;
}

class A {

	this(){
	}
}

class B {

	@Inject A a;

	mixin(injections!(B));

}

---
  when compiling this code this is the output I get:
----

Compiling using dmd...
injections for : B
member: B.a
B.a attributes are tuple()
member: B.toString
B.toString attributes are tuple()
member: B.toHash
B.toHash attributes are tuple()
member: B.opCmp
B.opCmp attributes are tuple()
member: B.opEquals
B.opEquals attributes are tuple()
member: B.Monitor
B.Monitor attributes are tuple()
member: B.factory
B.factory attributes are tuple()
------

B.a attributes are an empty tuple even though the member is 
annotated with @Inject.
Any ideas why?


More information about the Digitalmars-d-learn mailing list