Bug 16584 – Local import ineffective for mixin templates
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-10-03T22:03:00Z
Last change time
2016-10-03T22:27:29Z
Assigned to
nobody
Creator
m.bierlee
Comments
Comment #0 by m.bierlee — 2016-10-03T22:03:25Z
Consider the following code:
// func.d
module func;
public uint toNumber(string str) {
return str[0];
}
// mixins.d
module mixins;
import func;
mixin template bla(string number) {
uint globalBla = toNumber(number);
}
// app.d
module app;
import mixins;
public void main() {
mixin bla!"aaa";
}
When compiled, the following compilation error is shown:
src\mixins.d(6,19): Error: undefined identifier 'toNumber'
src\app.d(6,2): Error: mixin app.main.bla!"aaa" error instantiating
I expected the import of module 'func' to be only needed at the site of the mixin declaration, not where the mixin is used.
Publicly importing module 'func' in mixins.d works around the problem, as does importing module 'func' in app.d.
Comment #1 by m.bierlee — 2016-10-03T22:23:51Z
From the documentation:
"Unlike a template instantiation, a template mixin's body is evaluated within the scope where the mixin appears, not where the template declaration is defined."
So this is actually by design. Closed the issue.
Comment #2 by m.bierlee — 2016-10-03T22:27:29Z
For the record: the best solution is to import the needed modules within the mixin:
mixin template bla(string number) {
import func;
uint globalBla = toNumber(number);
}
This keeps within the scope of the mixin body.