www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Specialized template in different module

reply John <johnch_atms hotmail.com> writes:
If I define a template in one module, and specialize it in a 
second module, the compiler doesn't like it when I try to call a 
function using the template.

   module one;

   struct Test(T) {}

   void testing(T)(Test!T t) {}

   module two;

   struct Test(T : int) {}

   void main() {
     Test!int i;
     testing!int(i);
   }

Output:
   error : testing (Test!int t) is not callable using argument 
types (Test!int)

If I put everything in the same module it works. So are template 
specializations limited to the same module?
Mar 14 2016
next sibling parent ag0aep6g <anonymous example.com> writes:
On 14.03.2016 09:42, John wrote:
    module one;

    struct Test(T) {}

    void testing(T)(Test!T t) {}

    module two;

    struct Test(T : int) {}

    void main() {
      Test!int i;
      testing!int(i);
    }

 Output:
    error : testing (Test!int t) is not callable using argument types
 (Test!int)

 If I put everything in the same module it works. So are template
 specializations limited to the same module?
The two `Test` templates are completely unrelated to each other. `one.testing` only accepts `one.Test`, it isn't aware of `two.Test` at all. You can bring them together with `alias`: ---- module one; static import two; alias Test = two.Test; struct Test(T) {} void testing(T)(Test!T t) {} ---- `one.testing` still only accepts `one.Test`, but `one.Test` now includes `two.Test`, so it works. Sadly, dmd doesn't like it when you put the alias line behind the struct line. I think that's a bug. Filed it here: https://issues.dlang.org/show_bug.cgi?id=15795
Mar 14 2016
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On Monday, 14 March 2016 at 08:42:58 UTC, John wrote:
 If I define a template in one module, and specialize it in a 
 second module, the compiler doesn't like it when I try to call 
 a function using the template.
 If I put everything in the same module it works. So are 
 template specializations limited to the same module?
Package and module names are mangled into symbol names, which means templates declare in different modules will always be different templates. It's what allows you to disambiguate between conflicting symbols.
Mar 14 2016