templates in functions

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sat Jul 28 04:16:02 PDT 2007

BCS wrote:

> Why can't templates be put into functions like this
> |void foo()
> |{
> |  template Bar(int i){int Bar;}
> |
> |  Bar!(5) = 7;
> |  Bar!(12) = 3;
> |  writef("%s, %s\n", Bar!(5), Bar!(12));// prints "7, 3"
> |}
> this would be vary useful in templated code where a number of variables
> need to be declared outside of the scope where they will be used

Templates seem to be tightly anchored to the module level at the moment so
this isn't possible. But what you're doing here is pretty straightforward
extension to the current syntax.

I have a suggestion. Why not collect a list of necessary metaprogramming use
cases / constructs (also those that are not possible today) and model a new
system that covers them in a clean way (in case you're not doing something
like that already <g>). Although it may be by pure luck that many of the
current constructs are fixable incrementally at least to some extend. C++
templates are an abomination of the complete metaprogramming frameworks
that languages like Lisp possessed. Currently implementation difficulties
seem to drive the language evolution, which is quite unfortunate in my
opinion. :-/

More information about the Digitalmars-d-learn mailing list