www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiling Templates

reply Walter Bright <newshound1 digitalmars.com> writes:
http://www.reddit.com/r/programming/comments/8wgak/compiling_templates/
Jun 28 2009
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Walter Bright:
 http://www.reddit.com/r/programming/comments/8wgak/compiling_templates/
It looks like a quite complex machinery. From the article:
But there is a rule in C++ and D that a template with a specific set of
arguments can only have one instantiation for the whole program. This means
that if the template has already been instantiated with the same arguments, we
can simply point to that instantiation and we're done.<
Templates are as functions with the "pure" attribute, so the same input gives the same output. Most of the things you can do with templates can be done with functions (even not pure ones!) run at compile-time that are also allowed to use (and receive/return) a type of compile-time variables of type "type" :-) I think this may lead to a language that's cleaner than C++/D-style templates. Bye, bearophile
Jun 28 2009
parent reply Kagamin <spam here.lot> writes:
bearophile Wrote:

 Most of the things you can do with templates can be done with functions (even
not pure ones!) run at compile-time that are also allowed to use (and
receive/return) a type of compile-time variables of type "type" :-) I think
this may lead to a language that's cleaner than C++/D-style templates.
 
lisp?
Jul 02 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Kagamin:

 bearophile Wrote:
 
 Most of the things you can do with templates can be done with functions (even
not pure ones!) run at compile-time that are also allowed to use (and
receive/return) a type of compile-time variables of type "type" :-) I think
this may lead to a language that's cleaner than C++/D-style templates.
 lisp?
My experience of Lisp is quite limited. How can you do that with lisp? I think we are talking about different things. Bye, bearophile
Jul 02 2009
parent Kagamin <spam here.lot> writes:
bearophile Wrote:

 Kagamin:
 
 bearophile Wrote:
 
 Most of the things you can do with templates can be done with functions (even
not pure ones!) run at compile-time that are also allowed to use (and
receive/return) a type of compile-time variables of type "type" :-) I think
this may lead to a language that's cleaner than C++/D-style templates.
 lisp?
My experience of Lisp is quite limited. How can you do that with lisp? I think we are talking about different things.
Mine is zero :) From what I heard types in lisp are like any other symbols, no syntactic difference. You should only check you invoke right functions on them.
Jul 02 2009
prev sibling next sibling parent "Joel C. Salomon" <joelcsalomon gmail.com> writes:
Walter Bright wrote:
 http://www.reddit.com/r/programming/comments/8wgak/compiling_templates/
Nice & informative. Now if there were only such a readable description of how exceptions work… —Joel Salomon
Jun 29 2009
prev sibling parent "Saaa" <empty needmail.com> writes:
Always nice to see the inner workings, thanks.

Here a few things that bugged me:
Seeing templates as runtime functions, making the whole instantiation thing 
kind of strange :)
Trying to use implicitly deduces parameters for function templates.
Writing Foo(d) iso Foo!(int [][])(d).

--
template Foo(T: T[])
{
  void Foo(T array)
  {
    writefln(T.stringof);
  }
}

void main()
{
  int[] d;
  Foo!(int [][])(d);
//intuitively I wrote int[] at first as that is what I want T to be
} 
Jun 29 2009