On Friday, 31 May 2013 at 15:35:54 UTC, Jonathan M Davis wrote:
The situation with templates is basically the same as it is with C++. They have to go in the interface/header file and always will. There's no way around that, because the code importing the header/interface file actually needs the full definition. If the compiler supported some sort of intermediate format, then you could provide partially compiled templates, but pretty much all of the information would still have to be there (just in a less human readable format), and you can't even partially compile much of a template without the arguments to instantiate it with anyway. So, you'd pretty much just be putting the same template definition in a binary format instead of text, which wouldn't
stop much of anyone from figuring out what they looked like.

- Jonathan M Davis

Actually dmd for code importing module doesn't need full definition, it only inserts symbols referring to external object files. This allows to split template declaration and definition with restriction that module with template definition emits referred symbols (which restricts applicability since due to separate compilation model it is impossible to know beforehand template parameters). This also can be viewed as 'linker' template constraint, if you pass not expected parameters you will have link error.

import foo;
import std.stdio;

class Me : BiiA
{
   override void foo()
   {
      writeln("main");
   }
}


void main()
{
   BiiA b = new Me;
   b.foo();
   b.get();
}
--- foo.di-------
template bar(T, U)
{
   U baz();
   class A(T)
   {
      T t;
      T get();
      void foo();
   }
}

alias bar!(int, int) Bii;
alias Bii.A!int BiiA;
---foo.d ----
import std.stdio;

template bar(T, U)
{
   U baz();
   class A(T)
   {
      T t;
      T get()
      {
         writeln("foo");
         return t;
      }
      void foo()
      {
         writeln("foo");
      }
   }
}

alias bar!(int, int) Bii;
alias Bii.A!int BiiA;
-----
dmd main.d // Error undefined reference to ...
dmd main.d foo.o //works
./main
main
foo

Reply via email to