Infer function template parameters
Steven Schveighoffer
schveiguy at yahoo.com
Fri Sep 21 08:05:09 PDT 2012
On Thu, 20 Sep 2012 15:57:47 -0400, Jonas Drewsen <jdrewsen at nospam.com>
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?
Although I like it, I wonder if it works in D's context free grammar.
Timon probably would know best...
I came up with this code, which compiles today:
import std.stdio;
alias int x;
void foo(x) {}
void foo2(string x) {writeln(x);}
void main()
{
foo(1);
foo2("hello");
}
Under your proposal, if we shorten foo2 to foo2(x), what happens? Does it
become just like foo? Or does it turn into a template? Or is it an error?
Note that just because some syntax isn't valid doesn't mean it should be
utilized for a valid use. That can result in code compiling and meaning
something completely different than you expect.
-Steve
More information about the Digitalmars-d
mailing list