Topic: switch (and other control flow) as expressions? -
Author: walter1234 <walter2bz@gmail.com>
Date: Sun, 15 Jun 2014 14:23:14 -0700 (PDT)
Raw View
------=_Part_1044_32866472.1402867394242
Content-Type: text/plain; charset=UTF-8
After using rust for a while, one of the nice features is the expression
based syntax.
It helps you re-order code to avoid un-initialized variables.
So how about giving 'break' an expression, similar to return.
auto x= switch (something) {
case FOO: break 1;
case BAR: break 2;
default: break 3;
}
And how about if it was available for loops aswell; avoids some 'did we
break' flags. Take the 'for-else' construct of python.
auto result= for (x : &foo) {
if (something(x) )
break x;
} else { // for-else clause executed there
is no break out of the loop
break -1;
}
--
---
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 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/.
------=_Part_1044_32866472.1402867394242
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>After using rust for a while, one of the nice feature=
s is the expression based syntax.</div><div>It helps you re-order code to a=
void un-initialized variables.</div><div><br></div><div>So how about giving=
'break' an expression, similar to return.</div><div><br></div><div><br></d=
iv><div> auto x=3D switch (something) {</div><div> =
case FOO: break 1;</div><div> &nb=
sp; case BAR: break 2;</div><div> =
default: break 3;</div><div> =
} <br></div><div><br></div><div><br></div><div>And how about if it was=
available for loops aswell; avoids some 'did we break' flags. Take the 'fo=
r-else' construct of python.</div><div><br></div><div> auto res=
ult=3D for (x : &foo) {</div><div> if (=
something(x) )</div><div> bre=
ak x;</div><div> } else { &n=
bsp; // for-else cl=
ause executed there is no break out of the loop</div><div> &nb=
sp; break -1;</div><div> }</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" 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_1044_32866472.1402867394242--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Mon, 16 Jun 2014 00:30:59 +0300
Raw View
On 16 June 2014 00:23, walter1234 <walter2bz@gmail.com> wrote:
> After using rust for a while, one of the nice features is the expression
> based syntax.
> It helps you re-order code to avoid un-initialized variables.
>
> So how about giving 'break' an expression, similar to return.
>
>
> auto x= switch (something) {
> case FOO: break 1;
> case BAR: break 2;
> default: break 3;
> }
We already have that, you just write
auto x = [&]{ switch (something) {
case FOO: return 1;
case BAR: return 2;
default: return 3;
}}();
> And how about if it was available for loops aswell; avoids some 'did we
> break' flags. Take the 'for-else' construct of python.
>
> auto result= for (x : &foo) {
> if (something(x) )
> break x;
> } else { // for-else clause executed there is
> no break out of the loop
> break -1;
> }
This is
http://cplusplus.github.io/EWG/ewg-active.html#57
--
---
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 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/.
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sun, 15 Jun 2014 23:31:24 +0200
Raw View
--20cf30223e49f0d07704fbe6a128
Content-Type: text/plain; charset=UTF-8
On Sun, Jun 15, 2014 at 11:23 PM, walter1234 <walter2bz@gmail.com> wrote:
> After using rust for a while, one of the nice features is the expression
> based syntax.
> It helps you re-order code to avoid un-initialized variables.
>
> So how about giving 'break' an expression, similar to return.
>
>
> auto x= switch (something) {
> case FOO: break 1;
> case BAR: break 2;
> default: break 3;
> }
>
>
Just use a lambda:
auto x = [&] { switch (something) {
case FOO: return 1;
case BAR: return 2;
default: return 3;
}();
>
> And how about if it was available for loops aswell; avoids some 'did we
> break' flags. Take the 'for-else' construct of python.
>
> auto result= for (x : &foo) {
> if (something(x) )
> break x;
> } else { // for-else clause executed there
> is no break out of the loop
> break -1;
> }
>
and again, use a lambda.
> auto result= for (x : &foo) {
> if (something(x) )
> break x;
> } else { // for-else clause executed there
> is no break out of the loop
> break -1;
> }
>
>
> --
>
> ---
> 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
> 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/.
>
--
---
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 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/.
--20cf30223e49f0d07704fbe6a128
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><br><div class=3D"gmail=
_quote">On Sun, Jun 15, 2014 at 11:23 PM, walter1234 <span dir=3D"ltr"><=
<a href=3D"mailto:walter2bz@gmail.com" target=3D"_blank">walter2bz@gmail.co=
m</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;p=
adding-left:1ex"><div dir=3D"ltr"><div>After using rust for a while, one of=
the nice features is the expression based syntax.</div>
<div>It helps you re-order code to avoid un-initialized variables.</div><di=
v><br></div><div>So how about giving 'break' an expression, similar=
to return.</div><div><br></div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0aut=
o x=3D switch (something) {</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 case FOO: =C2=A0 break 1;</div><div>=C2=A0=
=C2=A0 =C2=A0 =C2=A0 case BAR: =C2=A0 break 2;</div><div>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 default: =C2=A0 =C2=A0 =C2=A0 =C2=A0break 3;</div><div>=C2=A0 =
=C2=A0 =C2=A0}=C2=A0<br></div><div><br></div></div></blockquote><div><br></=
div><div>Just use a lambda:</div>
<div><br></div><div>=C2=A0 =C2=A0 auto x =3D [&] { switch (something) {=
</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 case FOO: return 1;</div><div=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 case BAR: return 2;</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 default: =C2=A0 =C2=A0 =C2=A0return 3;</div><di=
v>=C2=A0 =C2=A0 }();</div><div><br>
</div><div><br></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(=
204,204,204);border-left-style:solid;padding-left:1ex"><div dir=3D"ltr"><di=
v></div>
<div><br></div><div>And how about if it was available for loops aswell; avo=
ids some 'did we break' flags. Take the 'for-else' construc=
t of python.</div><div><br></div><div>=C2=A0 =C2=A0auto result=3D for =C2=
=A0(x : &foo) {</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (something(x) =C2=A0)</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break x;</div><div>=C2=A0 =C2=A0} else { =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 // for-else clause executed there is no break out =
of the loop</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0break -1;</div><div>=C2=A0=
=C2=A0}</div>
</div></blockquote><div><br></div><div>and again, use a lambda.</div><div><=
div>=C2=A0 =C2=A0=C2=A0<br></div><div>=C2=A0</div><blockquote class=3D"gmai=
l_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-lef=
t-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir=3D"ltr"><div>=C2=A0 =C2=A0auto result=3D for =C2=A0(x : &foo) =
{</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (something(x) =C2=A0)</div><div>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break x;</div><div>=C2=A0 =C2=A0} =
else { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // for-else clause executed there is no bre=
ak out of the loop</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0break -1;</div><div>=C2=A0 =C2=A0}</div><di=
v><br></div></div></blockquote></div><div>=C2=A0</div><blockquote class=3D"=
gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border=
-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=3D""><font color=3D"#888888">
<p></p>
-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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" target=3D"_=
blank">std-proposals+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>
</font></span></blockquote></div><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" 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 />
--20cf30223e49f0d07704fbe6a128--
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sun, 15 Jun 2014 23:38:40 +0200
Raw View
--90e6ba3fcd51e5ee2b04fbe6bbb8
Content-Type: text/plain; charset=UTF-8
On Sun, Jun 15, 2014 at 11:30 PM, Ville Voutilainen <
ville.voutilainen@gmail.com> wrote:
> > And how about if it was available for loops aswell; avoids some 'did we
> > break' flags. Take the 'for-else' construct of python.
> >
> > auto result= for (x : &foo) {
> > if (something(x) )
> > break x;
> > } else { // for-else clause executed
> there is
> > no break out of the loop
> > break -1;
> > }
>
>
> This is
> http://cplusplus.github.io/EWG/ewg-active.html#57
>
auto result = [&]{
for (x : &foo)
if (something(x))
return x;
return -1;
}();
or did I miss something?
--
---
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 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/.
--90e6ba3fcd51e5ee2b04fbe6bbb8
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
un, Jun 15, 2014 at 11:30 PM, Ville Voutilainen <span dir=3D"ltr"><<a hr=
ef=3D"mailto:ville.voutilainen@gmail.com" target=3D"_blank">ville.voutilain=
en@gmail.com</a>></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 class=3D"">> And how about if it was=
available for loops aswell; avoids some 'did we<br></div><div class=3D=
"">
> break' flags. Take the 'for-else' construct of python.<br>
><br>
> =C2=A0 =C2=A0auto result=3D for =C2=A0(x : &foo) {<br>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0if (something(x) =C2=A0)<br>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0break x;<br>
> =C2=A0 =C2=A0} else { =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // for-else clause execut=
ed there is<br>
> no break out of the loop<br>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0break -1;<br>
> =C2=A0 =C2=A0}<br>
<br>
<br>
</div>This is<br>
<a href=3D"http://cplusplus.github.io/EWG/ewg-active.html#57" target=3D"_bl=
ank">http://cplusplus.github.io/EWG/ewg-active.html#57</a><br>
<div class=3D"HOEnZb"><div class=3D"h5"></div></div></blockquote></div><br>=
</div><div class=3D"gmail_extra">auto result =3D [&]{<br></div><div cla=
ss=3D"gmail_extra">=C2=A0 =C2=A0 for (x : &foo)</div><div class=3D"gmai=
l_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (something(x))</div>
<div class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return=
x;</div><div class=3D"gmail_extra"><br></div><div class=3D"gmail_extra">=
=C2=A0 =C2=A0 return -1;</div><div class=3D"gmail_extra">}();</div><div cla=
ss=3D"gmail_extra"><br></div><div class=3D"gmail_extra">
or did I miss something?</div><div class=3D"gmail_extra"><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" 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 />
--90e6ba3fcd51e5ee2b04fbe6bbb8--
.
Author: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Mon, 16 Jun 2014 00:31:05 +0200
Raw View
2014-06-15 23:38 GMT+02:00 Andrew Tomazos <andrewtomazos@gmail.com>:
> On Sun, Jun 15, 2014 at 11:30 PM, Ville Voutilainen
> <ville.voutilainen@gmail.com> wrote:
>>
>> > And how about if it was available for loops aswell; avoids some 'did we
>> > break' flags. Take the 'for-else' construct of python.
>> >
>> > auto result= for (x : &foo) {
>> > if (something(x) )
>> > break x;
>> > } else { // for-else clause executed
>> > there is
>> > no break out of the loop
>> > break -1;
>> > }
>>
>>
>> This is
>> http://cplusplus.github.io/EWG/ewg-active.html#57
>
>
> auto result = [&]{
> for (x : &foo)
> if (something(x))
> return x;
>
> return -1;
> }();
>
> or did I miss something?
>
Yes, because it won't allow to `return` from the enclosing function.
--
---
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 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/.
.
Author: Thiago Macieira <thiago@macieira.org>
Date: Sun, 15 Jun 2014 16:29:46 -0700
Raw View
Em seg 16 jun 2014, =E0s 00:31:05, Johannes Schaub escreveu:
> Yes, because it won't allow to `return` from the enclosing function.
It wasn't intended in the example.
--=20
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
--=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/.
.
Author: walter1234 <walter2bz@gmail.com>
Date: Mon, 16 Jun 2014 08:05:59 -0700 (PDT)
Raw View
------=_Part_71_31193685.1402931159280
Content-Type: text/plain; charset=UTF-8
Ok, lambdas help; but they still don't seem to cure 'rust match-envy'; (as
you can imagine i'm torn between the two..)
Imagine something like this:-
auto foo=virtual switch (x:some_expr()) {
case Foo: break (expr1(x));
case Bar: break (expr2(x));
case Baz: break (expr3(x));
}
desugars as..
auto foo=[]{
auto tmp=some_expr();
if (auto x= dynamic_cast<Foo*> tmp) {
return expr1(x);
}
if (auto x= dynamic_cast<Bar*> tmp) {
return expr2(x);
}
if (auto x= dynamic_cast<Baz*> tmp) {
return expr3(x);
}
}();
I guess you could do something with #defines to reduce the noise, or lambas
passed to an expression template. but even that is still quite noisy.
I think the compiler uses the latter pattern a bit, so if the language had
a dedicated feature it would help its own implementation.
I suppose an alternative would be to reduce the noise of the lambdas, with
something like swifts' lambda parameter sugar.
--
---
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 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/.
------=_Part_71_31193685.1402931159280
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Ok, lambdas help; but they still don't seem to cure 'rust =
match-envy'; (as you can imagine i'm torn between the two..)<div>Imagine so=
mething like this:-</div><div><br><div>auto foo=3Dvirtual switch (x:some_ex=
pr()) {</div><div> case Foo: break (expr1(x));</div><div>=
case Bar: break (expr2(x));</div><div> case =
Baz: break (expr3(x));</div><div>}</div><div><br></div><div>desugars as..</=
div><div><br></div><div>auto foo=3D[]{</div><div> auto tmp=3Ds=
ome_expr();</div><div> if (auto x=3D dynamic_cast<Foo*> =
tmp) {</div><div> return expr1(x);</div><d=
iv> }</div><div><div> if (auto x=3D dynamic_cast&=
lt;Bar*> tmp) {</div><div> return expr2=
(x);</div><div> }</div><div><div> if (auto x=3D d=
ynamic_cast<Baz*> tmp) {</div><div> =
return expr3(x);</div><div> }</div></div><div>}();</div><div><=
br></div><div>I guess you could do something with #defines to reduce the no=
ise, or lambas passed to an expression template. but even that is still qui=
te noisy.</div><div>I think the compiler uses the latter pattern a bit, so =
if the language had a dedicated feature it would help its own implementatio=
n.</div><div><br></div></div></div><div>I suppose an alternative would be t=
o reduce the noise of the lambdas, with something like swifts' lambda param=
eter sugar.</div><div><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" 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_71_31193685.1402931159280--
.