Topic: Proposal: extend "switch" with syntax for implicit "break
Author: kevingrant.engineer@gmail.com
Date: Fri, 15 Nov 2013 09:01:13 -0800 (PST)
Raw View
------=_Part_476_5483359.1384534873197
Content-Type: text/plain; charset=ISO-8859-1
*Proposal:* Add a new syntax to explicitly invert the standard fall-through
behavior of a switch so that each case has an implicit break. In addition,
add a syntax to make fall-through explicit in these kinds of switches.
*Rationale:* A break is an exceedingly common requirement in a switchstatement. In my experience working on various projects over many years,
the standard fall-through behavior is not only extremely rare but when it's
used it invariably comes with a comment like "intentional fall-through" to
remind the reader that the fall-through hasn't been allowed by mistake.
The cost in debugging a missing break in a case can be high, as it may lead
to subtle misbehavior.
*Suggested Syntax:* Reuse the auto, break, and goto keywords. Reuse the
fundamental structure of a switch statement (aside from no break and the
new annotations, the rest remains the same).
Example:
switch auto break (myValue) {
case ABC:
doSomething();
// "break" is implicit here
case DEF:
case GHI:
doSomethingElse();
// "break" is implicit here
case JKL:
doJKLAction();
goto case MNO; // in auto-break switch this is explicit fall-through
case MNO:
doJKLMNOAction();
// "break" is implicit here
default:
doTheDefaultThing();
// "break" is implicit here
}
--
---
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_476_5483359.1384534873197
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><b>Proposal:</b> Add a new syntax to explicitly invert the=
standard fall-through behavior of a <span style=3D"font-family: courier ne=
w,monospace;">switch</span> so that each case has an implicit <span style=
=3D"font-family: courier new,monospace;">break</span>. In addition, a=
dd a syntax to make fall-through explicit in these kinds of switches.<br><b=
r><b>Rationale:</b> A <span style=3D"font-family: courier new,monospace;">b=
reak</span> is an exceedingly common requirement in a <span style=3D"font-f=
amily: courier new,monospace;">switch</span> statement. In my experie=
nce working on various projects over many years, the standard fall-through =
behavior is not only extremely rare but when it's used it invariably comes =
with a comment like "intentional fall-through" to remind the reader that th=
e fall-through hasn't been allowed by mistake. The cost in debugging =
a missing <span style=3D"font-family: courier new,monospace;">break</span> =
in a case can be high, as it may lead to subtle misbehavior.<br><br><b>Sugg=
ested Syntax:</b> Reuse the <span style=3D"font-family: courier new,monospa=
ce;">auto</span>, <span style=3D"font-family: courier new,monospace;">break=
</span>, and <span style=3D"font-family: courier new,monospace;">goto</span=
> keywords. Reuse the fundamental structure of a <span style=3D"font-=
family: courier new,monospace;">switch</span> statement (aside from no <spa=
n style=3D"font-family: courier new,monospace;">break</span> and the new an=
notations, the rest remains the same).<br><div style=3D"margin-left: 40px;"=
>Example:<br><div style=3D"margin-left: 40px;"><font face=3D"courier new,mo=
nospace">switch auto break (<font face=3D"courier new,monospace">myValue) {=
<br><font face=3D"courier new,monospace">case<font face=3D"courier new,mono=
space"> <font face=3D"courier new,monospace">ABC</font>:<br></font></font><=
/font></font><div style=3D"margin-left: 40px;"><tt>doSomething();<br>// "br=
eak" is implicit here<br></tt></div><tt>case DEF:</tt><br><tt>case GHI:<br>=
</tt><div style=3D"margin-left: 40px;"><tt>doSomethingElse();<br>// "break"=
is implicit here<br></tt></div><tt>case JKL:<br></tt><div style=3D"margin-=
left: 40px;"><tt>doJKLAction();</tt><br></div><div style=3D"margin-left: 40=
px;"><tt>goto case MNO;</tt><tt><tt> // in auto-break switch this is explic=
it fall-through</tt><br></tt></div><tt>case MNO:</tt><br><div style=3D"marg=
in-left: 40px;"><tt>doJKLMNOAction();</tt><br></div><div style=3D"margin-le=
ft: 40px;"><tt>// "break" is implicit here</tt><br></div><tt>default:<br></=
tt><div style=3D"margin-left: 40px;"><tt>doTheDefaultThing();<br>// "break"=
is implicit here<br></tt></div><font face=3D"courier new,monospace"><font =
face=3D"courier new,monospace"><font face=3D"courier new,monospace">}</font=
><br></font></font></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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_476_5483359.1384534873197--
.