Template functions, can we make it more simple?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Aug 2 15:46:46 PDT 2013


On Fri, Aug 02, 2013 at 03:19:33PM -0700, Andrei Alexandrescu wrote:
> On 8/2/13 3:17 PM, Timon Gehr wrote:
> >On 08/03/2013 12:06 AM, Andrej Mitrovic wrote:
> >>On 8/2/13, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> >>>auto fun(auto x, auto y) { … }
> >>>
> >>>Truth be told, at the time of that decision
> >>>parameter names (viz. x and y) could not be used in template
> >>>constraints. Now they could, so in a way that reopens the question.
> >>
> >>You'd still have to use typeof(x) and typeof(y) to extract the types,
> >>so even though you end up making the template declaration simpler you
> >>also make the constraint more complicated.
> >>...
> >
> >bool compare(a,b) if(__traits(compiles,a<b)) { return a<b; }
> 
> Direct use of __traits is unrecommended outside the stdlib. I had
> this pattern in mind:
> 
> bool compare(auto a, auto b) if (is(typeof(a < b) : bool)) { ... }
[...]

I like this syntax.

I'm worried, though, about how it will interact with explicit template
parameters. E.g., how would you express this:

	bool func(R,T,U)(T t, U u) { ... }

in the new syntax?

	bool func(R)(auto t, auto u) { ... }

?

What if we have variadics on either side?

I'm still on the fence as to whether we should add the new syntax, nice
as it is. The current syntax is far more unambiguous, and allows you to
specify signature constraints on the input types directly, as well as
use it in the return type spec, without needing to say typeof(t) or
typeof(u).


T

-- 
"The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." -- Bertrand Russell. "How come he didn't put 'I think' at the end of it?" -- Anonymous


More information about the Digitalmars-d mailing list