www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - implicit template constraint notation

reply Timothee Cour <thelastmammoth gmail.com> writes:
--089e0118289ee9138604dec4b027
Content-Type: text/plain; charset=ISO-8859-1

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.

--089e0118289ee9138604dec4b027
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>A)</div><div>I&#39;d like to simplify notation of template function de=
clarations involving only single-argument boolean template constraints as f=
ollows:</div><div><br></div><div>example:</div><div>A1: &#39;auto myfunctio=
n ( isSomeString a, isInputRange b) {...}&#39;</div>
<div><br></div><div>would be rewritten by compiler as:</div><div><div>A2: &=
#39;auto myfunction(T0,T1) (T0 a, T1 b) if(isSomeString!T1 a &amp;&amp; isI=
nputRange!T b) {...}&#39;</div></div><div><br></div><div>IMO, A1 is less ve=
rbose and clearer than A2.=A0</div>
<div><br></div><div>Obviously, more complex template constraints would stil=
l require the full syntax, but I&#39;d argue this case is the most common.<=
/div><div><br></div><div><br></div><div>More generally, the compiler would =
rewrite:</div>
<div><div>&#39;auto myfunction(Tj...) (modifier_i Ei ai,...) if(condition(T=
j) ) {...}&#39;</div></div><div>as:</div><div>&#39;auto myfunction(Tj...,T&=
#39;k...) (modifier_i E&#39;i ai,...) if(condition(Tj) &amp;&amp; Ek!T&#39;=
k &amp;...) {...}&#39;</div>
<div>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&#39=
;i as type T&#39;i, else E&#39;i=3DEi; and k ranges over the list of indexe=
s i that represent the former case.</div>
<div><br></div><div><br></div><div>B)</div><div>Secondly, ddoc doesn&#39;t =
generate template constraints or does so very inconsistently :</div><div>in=
 <a href=3D"http://dlang.org/phobos/std_algorithm.html">http://dlang.org/ph=
obos/std_algorithm.html</a> we have:</div>
<div>template map(fun...) if (fun.length &gt;=3D 1);</div><div>but all othe=
r template constraints are omitted, eg:</div><div><div>void fill(Range, Val=
ue)(Range range, Value filler); // template constraint omitted.</div></div>
<div>Using the notation proposed in A, wherever applicable, would make docu=
mentation clear.</div><div><br></div>

--089e0118289ee9138604dec4b027--
Jun 09 2013
next sibling parent "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
prev sibling next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--089e010d9f221c174004ded38fe6
Content-Type: text/plain; charset=ISO-8859-1

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

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 --089e010d9f221c174004ded38fe6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div><div class=3D"gmail_quote">On Mon, Jun 10, 2013 at 2:19 AM, bearophile= <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com" target= =3D"_blank">bearophileHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote c= lass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;= padding-left:1ex"> Timothee Cour:<div class=3D"im"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> A)<br> I&#39;d like to simplify notation of template function declarations involvi= ng<br> only single-argument boolean template constraints as follows:<br> <br> example:<br> A1: &#39;auto myfunction ( isSomeString a, isInputRange b) {...}&#39;<br> <br> would be rewritten by compiler as:<br> A2: &#39;auto myfunction(T0,T1) (T0 a, T1 b) if(isSomeString!T1 a &amp;&amp= ;<br> isInputRange!T b) {...}&#39;<br> <br> IMO, A1 is less verbose and clearer than A2.<br> <br> Obviously, more complex template constraints would still require the full<b= r> syntax, but I&#39;d argue this case is the most common.<br> </blockquote> <br></div> See:<br> <a href=3D"http://forum.dlang.org/thread/xaganckgcdkfcmjamogh forum.dlang.o= rg" target=3D"_blank">http://forum.dlang.org/thread/<u></u>xaganckgcdkfcmja= mogh forum.<u></u>dlang.org</a></blockquote><div><br></div><div>ah, great! = So I guess it must indeed be a good idea!<div> <div><br></div><div>from the link:</div><div>&gt; =A0If you have two or mor= e types, they must be the same (if you don&#39;t want this, you have to use= the normal longer syntax)</div><div><br></div><div>In what I suggest, the = restriction is much weaker so it&#39;d be more generally applicable: for ex= ample, &#39;auto myfunction ( isSomeString a, isInputRange b)&#39; would wo= rk in what I suggest but not with the proposal in the link. I don&#39;t thi= nk it adds any confusion.</div> <div><br></div><div>Should I draft a DIP?</div><div>I&#39;d like to get mor= e feedback before though.</div><div><br></div><div><br></div><div>I&#39;ll = also reply on the above link (CppNow 2013) for your second proposal from cp= pnow (i think variant does already that).</div> <div><br></div></div></div><div>=A0</div><blockquote class=3D"gmail_quote" = style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><di= v class=3D"im"> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> B)<br> Secondly, ddoc doesn&#39;t generate template constraints or does so very<br=

in <a href=3D"http://dlang.org/phobos/std_algorithm.html" target=3D"_blank"=
http://dlang.org/phobos/std_<u></u>algorithm.html</a> we have:<br>

but all other template constraints are omitted, eg:<br> void fill(Range, Value)(Range range, Value filler); // template constraint<= br> omitted.<br> Using the notation proposed in A, wherever applicable, would make documenta= tion clear.<br> </blockquote> <br></div> That sounds like a bug report for bugzilla.<br></blockquote><div><br></div>= <div>just filed:=A0<a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id= =3D10325">http://d.puremagic.com/issues/show_bug.cgi?id=3D10325</a></div><d= iv> <br></div></div></div> --089e010d9f221c174004ded38fe6--
Jun 10 2013
prev sibling 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