www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2125] New: Moving a template to a separate module breaks compilation

reply d-bugmail puremagic.com writes:

           Summary: Moving a template to a separate module breaks
           Product: D
           Version: 2.013
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: bartosz relisoft.com

---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;
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

May 23 2008
parent d-bugmail puremagic.com writes:

kamm-removethis incasoftware.de changed:

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID

------- Comment #1 from kamm-removethis incasoftware.de  2008-05-24 18:52
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.

May 24 2008