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:
http://d.puremagic.com/issues/show_bug.cgi?id=2125

           Summary: Moving a template to a separate module breaks
                    compilation
           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;
}
----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
.


-- 
May 23 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2125


kamm-removethis incasoftware.de changed:

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





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