On Sunday, 7 April 2019 at 05:24:38 UTC, Alex wrote:
Error: template instance `Reflect!(type)` cannot use local `type` as parameter to non-global template `Reflect(Ts...)()`

mixin(`import `~moduleName!(T)~`;`);    
mixin(`alias X = T.`~name~`;`);                 
super.Reflect!(X);

I realize X is local but I'm trying to figure out why it can't be passed to a "non-global" template.

- In DMD, objects may have at most one scope (which can be a class, struct, or stack frame).

- Instantiating a template with a scoped object makes the template have the same scope as the object.

- If a template is already scoped (non-global), then it thus cannot have a second scope. Hence the error.

IIRC there is an experimental pull request which removes this limitation.

Workarounds:

- Make either the template or the argument global (non-scoped)

- Instead of passing the object as a template parameter, pass it as a runtime parameter with a type encapsulating the object. Each runtime parameter may be scoped, thus bypassing the limitation. I'm not sure this applies to your use case, if the Reflect template needs to perform compile-time introspection on the parameter.

- If you just need to pass the type, typeof(X) should work of course.

Reply via email to