Infer function template parameters

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 20 14:04:43 PDT 2012


On Thursday, September 20, 2012 21:57:47 Jonas Drewsen wrote:
> In foreach statements the type can be inferred:
> 
> foreach (MyFooBar fooBar; fooBars) writeln(fooBar);
> same as:
> foreach (foobar; fooBars) writeln(fooBar);
> 
> This is nice and tidy.
> Wouldn't it make sense to allow the same for function templates
> as well:
> 
> auto min(L,R)(L a, R b)
> {
> return a < b;
> }
> 
> same as:
> 
> auto min(a,b)
> {
> return a < b;
> }
> 
> What am I missing (except some code that needs chaging because
> only param type and not name has been specified in t?

You don't want everything templated. Templated functions are fundamentally 
different. They don't exist until they're instantiated, and they're only 
instantiated because you call them. Sometimes, you want functions to always 
exist regardless of whether any of your code is calling them (particularly 
when dealing with libraries).

Another result of all of this is that templated functions can't be virtual, so 
your proposal would be a _huge_ problem for classes. Not to mention, errors 
with templated functions tend to be much nastier than with non-templated 
functions even if it's not as bad as C++. Also, your prosposal then means that 
we'd up with templated functions without template constraints as a pretty 
normal thing, which would mean that such functions would frequently get called 
with types that don't work with them. To fix that, you'd have to add template 
constraints to such functions, which would be even more verbose than just 
giving the types like we do now.

You really need to be able to control when something is templated or not. And 
your proposal is basically just a terser template syntax. Is it really all 
that more verbose to do

auto min(L, R)(L a, R b) {...}

rather than

auto min(a, b) {...}

And even if we added your syntax, we'd still need the current syntax, because 
you need to able to indicate which types go with which parameters even if it's 
just to say that two parameters have the same type.

Also, what happens if you put types on some parameters but not others? Are 
those parameters given templated types? If so, a simple type could silently 
turn your function into a templated function without you realizing it.

Then there's function overloading. If you wanted to overload a function in 
your proposal, you'd have to either still give the types or use template 
constraints, meaning that it can't be used with overloaded functions.

Another thing to consider is that in languages like Haskell where all 
parameter types are inferred, it's often considered good practice to give the 
types anyway (assuming that the language lets you - Haskell does), because the 
functions are then not only easier to understand, but the error messages are 
more sane.

So, I really don't think that this is a good idea. It's just a terser, less 
flexible, and more error-prone syntax for templates.

- Jonathan M Davis


More information about the Digitalmars-d mailing list