Bug 2125 – Moving a template to a separate module breaks compilation

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-05-23T14:46:00Z
Last change time
2015-06-09T01:19:25Z
Assigned to
bugzilla
Creator
bartosz

Comments

Comment #0 by bartosz — 2008-05-23T14:46:26Z
---File totype.d--- module totype; import std.string; template ToType (string s) { alias ToTypeHelper!(s).type ToType; } template ToTypeHelper (string s) { mixin ("alias " ~ s ~ " type;"); } ----File test.d--- import std.stdio; import std.string; import totype; // Concatenate strings with a separator string ctConcat (string [] arr, char sep) { assert (arr.length != 0); string result = ""; foreach (s; arr) result ~= s ~ sep; return result [0..$-1]; // remove the trailing sep } string Options (string [] bases) { if (bases.length == 0) return "NoLock"; return ctConcat (bases, '_'); } interface A_B {} void main () { ToType!(Options (["A","B"])) lockOptions; } ----Output---- c:\D\Work>dmd -oftest.exe test totype Error: identifier 'A_B' is not defined totype.d(6): template instance totype.ToTypeHelper!(s) error instantiating totype.d(6): Error: forward reference to 'A_B' ------- The error disappears when the contents of totype.d is copied directly to test.d .
Comment #1 by kamm-removethis — 2008-05-24T18:52:26Z
I think that is desired behaviour. Templates are generally instantiated in the scope they are declared in, consider: module A; void foo(T)() { writefln(); } -- import A; import std.stdio; void main() { foo!(void)(); } will also fail to compile. The appearance of a string mixin doesn't change that behaviour. You can use template mixins to instantiate templates in a different context.