mixin module template -> undefined identifier

David Nadlinger code at klickverbot.at
Thu Oct 3 11:10:56 PDT 2013


On Thursday, 3 October 2013 at 17:52:13 UTC, Robert Schadek wrote:
> // file: moduleA.d
>
> module A;
>
> public int func(T)() {
>     return mixin(T.stringof ~ ".fun()");
> }
>
> […]
>
> dmd moduleA.d moduleB.d -ofmix -unittest
> moduleA.d(4): Error: undefined identifier Foo
> moduleB.d(10): Error: template instance A.func!(Foo) error 
> instantiating

Yes, this is indeed very much by design – A does not import B, so 
there is no reason why the name "Foo" should exist in A.

To access the template parameter, just use it directly in the 
mixin (as in »mixin("T.fun()")«). I'm aware that the situation 
where you actually stumbled over this is probably a bit more 
complex, but in my experience an equivalent rewrite can almost 
always be performed quite easily once you have wrapped your head 
around the concept.

Oh, and for those of you keeping track, this is another example 
supporting my stance that using "stringof" for code generation is 
(almost) always a bad idea. The fact that experienced D coders 
seem to run into this trap quite frequently, judging from the 
fact that is by far not the first NG thread on this topic, seems 
to suggest that we should address this with a big red warning in 
the documentation, probably where string mixins are discussed. 
(The recently merged pull request warning about .stringof use is 
a first step, albeit it does so for a different reason.)

David


More information about the Digitalmars-d mailing list