www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - implicit template constraint notation

reply Timothee Cour <thelastmammoth gmail.com> writes:
A)
I'd like to simplify notation of template function declarations involving
only single-argument boolean template constraints as follows:

example:
A1: 'auto myfunction ( isSomeString a, isInputRange b) {...}'

would be rewritten by compiler as:
A2: 'auto myfunction(T0,T1) (T0 a, T1 b) if(isSomeString!T1 a &&
isInputRange!T b) {...}'

IMO, A1 is less verbose and clearer than A2.

Obviously, more complex template constraints would still require the full
syntax, but I'd argue this case is the most common.


More generally, the compiler would rewrite:
'auto myfunction(Tj...) (modifier_i Ei ai,...) if(condition(Tj) ) {...}'
as:
'auto myfunction(Tj...,T'k...) (modifier_i E'i ai,...) if(condition(Tj) &&
Ek!T'k &...) {...}'
where, for each i, if Ei is the name (in current scope) of a template that
has a single argument and that returns a boolean, then introduce E'i as
type T'i, else E'i=Ei; and k ranges over the list of indexes i that
represent the former case.


B)
Secondly, ddoc doesn't generate template constraints or does so very
inconsistently :
in http://dlang.org/phobos/std_algorithm.html we have:
template map(fun...) if (fun.length >= 1);
but all other template constraints are omitted, eg:
void fill(Range, Value)(Range range, Value filler); // template constraint
omitted.
Using the notation proposed in A, wherever applicable, would make
documentation clear.
Jun 09 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Timothee Cour:

 A)
 I'd like to simplify notation of template function declarations 
 involving
 only single-argument boolean template constraints as follows:

 example:
 A1: 'auto myfunction ( isSomeString a, isInputRange b) {...}'

 would be rewritten by compiler as:
 A2: 'auto myfunction(T0,T1) (T0 a, T1 b) if(isSomeString!T1 a &&
 isInputRange!T b) {...}'

 IMO, A1 is less verbose and clearer than A2.

 Obviously, more complex template constraints would still 
 require the full
 syntax, but I'd argue this case is the most common.
See: http://forum.dlang.org/thread/xaganckgcdkfcmjamogh forum.dlang.org
 B)
 Secondly, ddoc doesn't generate template constraints or does so 
 very
 inconsistently :
 in http://dlang.org/phobos/std_algorithm.html we have:
 template map(fun...) if (fun.length >= 1);
 but all other template constraints are omitted, eg:
 void fill(Range, Value)(Range range, Value filler); // template 
 constraint
 omitted.
 Using the notation proposed in A, wherever applicable, would 
 make documentation clear.
That sounds like a bug report for bugzilla. Bye, bearophile
Jun 10 2013
parent reply Timothee Cour <thelastmammoth gmail.com> writes:
On Mon, Jun 10, 2013 at 2:19 AM, bearophile <bearophileHUGS lycos.com>wrote:

 Timothee Cour:


  A)
 I'd like to simplify notation of template function declarations involving
 only single-argument boolean template constraints as follows:

 example:
 A1: 'auto myfunction ( isSomeString a, isInputRange b) {...}'

 would be rewritten by compiler as:
 A2: 'auto myfunction(T0,T1) (T0 a, T1 b) if(isSomeString!T1 a &&
 isInputRange!T b) {...}'

 IMO, A1 is less verbose and clearer than A2.

 Obviously, more complex template constraints would still require the full
 syntax, but I'd argue this case is the most common.
See: http://forum.dlang.org/thread/**xaganckgcdkfcmjamogh forum.**dlang.org<http://forum.dlang.org/thread/xaganckgcdkfcmjamogh forum.dlang.org>
ah, great! So I guess it must indeed be a good idea! from the link:
  If you have two or more types, they must be the same (if you don't want
this, you have to use the normal longer syntax) In what I suggest, the restriction is much weaker so it'd be more generally applicable: for example, 'auto myfunction ( isSomeString a, isInputRange b)' would work in what I suggest but not with the proposal in the link. I don't think it adds any confusion. Should I draft a DIP? I'd like to get more feedback before though. I'll also reply on the above link (CppNow 2013) for your second proposal from cppnow (i think variant does already that).
  B)
 Secondly, ddoc doesn't generate template constraints or does so very
 inconsistently :
 in http://dlang.org/phobos/std_**algorithm.html<http://dlang.org/phobos/s
d_algorithm.html>we have:
 template map(fun...) if (fun.length >= 1);
 but all other template constraints are omitted, eg:
 void fill(Range, Value)(Range range, Value filler); // template constraint
 omitted.
 Using the notation proposed in A, wherever applicable, would make
 documentation clear.
That sounds like a bug report for bugzilla.
just filed: http://d.puremagic.com/issues/show_bug.cgi?id=10325
Jun 10 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Timothee Cour:

 ah, great! So I guess it must indeed be a good idea!
I don't know. It's a cute idea, but I think it doesn't add a lot of value. What are its advantages in D beside reducing a little the amount of code?
 In what I suggest, the restriction is much weaker so it'd be 
 more generally
 applicable: for example, 'auto myfunction ( isSomeString a, 
 isInputRange
 b)' would work in what I suggest but not with the proposal in 
 the link. I
 don't think it adds any confusion.
"myfunction(isSomeString a, isInputRange b)" should work. The restriction was different, about code like: myfunction2(isInputRange a, isInputRange b) And then trying to instantiate myfunction2 with two types (for a and b) that are both input ranges but are two different types.
 Should I draft a DIP?
Feel free, but be prepared to not see lot of people interested in it.
 I'd like to get more feedback before though.
Right. Andrei is expert on this topic.
 just filed: http://d.puremagic.com/issues/show_bug.cgi?id=10325
I have added a note. It's good to help as much as possible the person that will write the patch :-) Bye, bearophile
Jun 10 2013