Topic: return type sugar (orthogonal to auto foo()


Author: walter lynsdale <walter2bz@gmail.com>
Date: Sat, 14 Jun 2014 03:37:41 +0100
Raw View
--f46d044280769a6e0d04fbc2ad0a
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

the go-like idea is interesting,
would the grammar work if you were able to initialise 'accumulator' with an
expression based on the inputs ?

I think the decltype(return) would have more uses , e.g. accessing
associated types aswell ?


On Fri, Jun 13, 2014 at 5:25 PM, Andrzej Krzemie=C5=84ski <akrzemi1@gmail.c=
om>
wrote:

>
> W dniu pi=C4=85tek, 13 czerwca 2014 18:24:28 UTC+2 u=C5=BCytkownik Andrze=
j
> Krzemie=C5=84ski napisa=C5=82:
>
>>
>> W dniu czwartek, 12 czerwca 2014 12:16:04 UTC+2 u=C5=BCytkownik walter12=
34
>> napisa=C5=82:
>>>
>>> Coming back to C++ from Rust, one thing begin to miss is the 2way type
>>> inference: its a nice choice on their part, you write the types in the
>>> signature, then the types in the body are inferred.
>>>
>>> But perhaps a significant amount of this use could be covered by having
>>> sugar to access the declared return type of a function within its own b=
ody.
>>>
>>> imagine
>>>
>>> auto foo(  args...  )->decltype(....some expression based on args...) {
>>>
>>>       return_type  accumulator;     // 'return_type' is a shortcut for
>>> whatever this functions declared return type is..
>>>       ... do stuff with accumulator...
>>>
>>>      return accumulator.
>>> }
>>>
>>> ways of doing that without a new keyword, perhaps 'decltype(return)' ,
>>> any other ideas..
>>> In some ways this would go beyond what rust currently does. (rust can't
>>> yet use an expression for the return type, although making it generic o=
ften
>>> works because of how the return types are inferred by callers)
>>>
>>
>> How about doing it the Go style:
>>
>> auto foo(  args...  ) -> decltype(....some expression based on args...)
>> accumulator
>> {
>>   do_stuff_with(accumulator);
>>   return accumulator.
>> }
>>
>
> Or even w/o return statement because it is obvious what we return:
>
> auto foo( args... ) -> decltype(....some expression based on args...)
> accumulator
> {
>    do_stuff_with(accumulator);
>    // no return (and no UB)
> }
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/a/isocpp.org/d/topic/std-proposals/wdX31-nWSnc/=
unsubscribe
> .
> To unsubscribe from this group and all its topics, send an email to
> std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

--f46d044280769a6e0d04fbc2ad0a
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">the go-like idea is interesting,=C2=A0<div>would the gramm=
ar work if you were able to initialise &#39;accumulator&#39; with an expres=
sion based on the inputs ?</div><div><br></div><div>I think the decltype(re=
turn) would have more uses , e.g. accessing associated types aswell ?</div>
</div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">On Fri,=
 Jun 13, 2014 at 5:25 PM, Andrzej Krzemie=C5=84ski <span dir=3D"ltr">&lt;<a=
 href=3D"mailto:akrzemi1@gmail.com" target=3D"_blank">akrzemi1@gmail.com</a=
>&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><br>W dniu pi=C4=85tek, 13 =
czerwca 2014 18:24:28 UTC+2 u=C5=BCytkownik Andrzej Krzemie=C5=84ski napisa=
=C5=82:<div class=3D""><blockquote style=3D"margin:0px 0px 0px 0.8ex;paddin=
g-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-=
left-style:solid" class=3D"gmail_quote">
<div dir=3D"ltr"><br>W dniu czwartek, 12 czerwca 2014 12:16:04 UTC+2 u=C5=
=BCytkownik walter1234 napisa=C5=82:<blockquote style=3D"margin:0px 0px 0px=
 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-widt=
h:1px;border-left-style:solid" class=3D"gmail_quote">
<div dir=3D"ltr">Coming back to C++ from Rust, one thing begin to miss is t=
he 2way type inference: its a nice choice on their part, you write the type=
s in the signature, then the types in the body are inferred.<div><br></div>
<div>But perhaps a significant amount of this use could be covered by havin=
g sugar to access the declared return type of a function within its own bod=
y.</div><div><br></div><div>imagine</div><div><br></div><div>auto foo( =C2=
=A0args... =C2=A0)-&gt;decltype(....some expression based on args...) {</di=
v>
<div><br></div><div>=C2=A0 =C2=A0 =C2=A0 return_type =C2=A0accumulator; =C2=
=A0 =C2=A0 // &#39;return_type&#39; is a shortcut for whatever this functio=
ns declared return type is..</div><div>=C2=A0 =C2=A0 =C2=A0 ... do stuff wi=
th accumulator...</div><div><br></div><div>
=C2=A0 =C2=A0 =C2=A0return accumulator.</div><div>}</div><div><br></div><di=
v>ways of doing that without a new keyword, perhaps &#39;decltype(return)&#=
39; , any other ideas..</div><div>In some ways this would go beyond what ru=
st currently does. (rust can&#39;t yet use an expression for the return typ=
e, although making it generic often works because of how the return types a=
re inferred by callers)</div>
</div></blockquote><div>=C2=A0</div><div>How about doing it the Go style:</=
div><div>=C2=A0</div><div>auto foo(=C2=A0 args...=C2=A0 ) -&gt; decltype(..=
...some expression based on args...) accumulator<br>{=C2=A0=C2=A0=C2=A0<br>=
=C2=A0 do_stuff_with(accumulator);<br>
=C2=A0 return accumulator.<br>}</div></div></blockquote><div>=C2=A0</div></=
div><div>Or even w/o return statement because it is obvious what we return:=
</div><div>=C2=A0</div><div><div class=3D"">auto foo(  args...  ) -&gt; dec=
ltype(....some expression based on args...) accumulator<br>
{   <br> =C2=A0=C2=A0 do_stuff_with(accumulator);<br></div>=C2=A0=C2=A0=C2=
=A0// no return (and no UB)<br>}</div></div><div class=3D"HOEnZb"><div clas=
s=3D"h5">

<p></p>

-- <br>
<br>
--- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/wdX31-nWSnc/unsubscribe" target=3D"_blan=
k">https://groups.google.com/a/isocpp.org/d/topic/std-proposals/wdX31-nWSnc=
/unsubscribe</a>.<br>

To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_blank">std-prop=
osals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--f46d044280769a6e0d04fbc2ad0a--

.


Author: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@gmail.com>
Date: Mon, 16 Jun 2014 04:29:58 -0700 (PDT)
Raw View
------=_Part_845_5997476.1402918198870
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



W dniu sobota, 14 czerwca 2014 04:37:43 UTC+2 u=C5=BCytkownik walter1234 na=
pisa=C5=82:
>
> the go-like idea is interesting,=20
> would the grammar work if you were able to initialise 'accumulator' with=
=20
> an expression based on the inputs ?
>

We are fantasizing about a potential addition, so we can decide on whatever=
=20
we want.


> I think the decltype(return) would have more uses , e.g. accessing=20
> associated types aswell ?
>

The following work today:

struct ToolBox
{
  typedef int Tool;
  // declarations
}

auto ToolBox::getTool() -> Tool // no need for ToolBox::Tool=20
{/*...*/}

auto ToolBox::make(X x, Y y) -> decltype(x + y)
{/*...*/}

Given that the trailing return type can access both names associated with=
=20
the class and argument names, it should be possible to have the "default=20
return variable" make use of these.

Regards,
&rzej=20

=20

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

------=_Part_845_5997476.1402918198870
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>W dniu sobota, 14 czerwca 2014 04:37:43 UTC+2 u=C5=
=BCytkownik walter1234 napisa=C5=82:<blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;"><div dir=3D"ltr">the go-like idea is interesting,&nbsp;<div>would the=
 grammar work if you were able to initialise 'accumulator' with an expressi=
on based on the inputs ?</div></div></blockquote><div><br>We are fantasizin=
g about a potential addition, so we can decide on whatever we want.<br><br>=
</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8=
ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><b=
r></div><div>I think the decltype(return) would have more uses , e.g. acces=
sing associated types aswell ?</div></div></blockquote><div><br>The followi=
ng work today:<br><br>struct ToolBox<br>{<br>&nbsp; typedef int Tool;<br>&n=
bsp; // declarations<br>}<br><br>auto ToolBox::getTool() -&gt; Tool // no n=
eed for ToolBox::Tool <br>{/*...*/}<br><br>auto ToolBox::make(X x, Y y) -&g=
t; decltype(x + y)<br>{/*...*/}<br><br>Given that the trailing return type =
can access both names associated with the class and argument names, it shou=
ld be possible to have the "default return variable" make use of these.<br>=
<br>Regards,<br>&amp;rzej <br><br>&nbsp;<br></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_845_5997476.1402918198870--

.