digitalmars.D.bugs - [Issue 4349] New: Deprecate automatic case fallthrough
- d-bugmail puremagic.com (39/39) Jun 19 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (73/73) Jun 21 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (12/12) Jun 21 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (11/11) Jun 21 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (9/9) Sep 28 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (13/13) Sep 28 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (10/10) Jan 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4349
- d-bugmail puremagic.com (22/23) Jan 02 2012 http://d.puremagic.com/issues/show_bug.cgi?id=4349
http://d.puremagic.com/issues/show_bug.cgi?id=4349
Summary: Deprecate automatic case fallthrough
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody puremagic.com
ReportedBy: bearophile_hugs eml.cc
Bugs caused by unwanted fall-through between cases in switch statement are
avoid them.
D2 has already in place all is necessary to avoid this source of problems. The
only missing part is to disallow cases that miss an explicit return, goto,
break, assert(0), exit(), or similar.
C code ported to D that assumes the fall-through will just raise errors that
can be solved adding "goto case;" at the end of the cases.
The usage of "goto case;" will not be excessive because D allows both ranged
cases and multiple with a comma that allow to compress switch code.
case 1, 2, 3:
case 1: .. case 3:
As for the multiple returns in a function this kind of code needs some care:
switch (x) {
case 0:
if (y)
goto case;
else
return x;
default:
break;
}
Bug 3536 shows an alternative proposal.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 19 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349
Sean Kelly and Michel Fortin have shown some code examples.
This D code:
switch (x) {
case 1: case 2: case 3: case 4:
doSomething();
break;
default:
whatever();
}
With the new proposal can be translated like this (this is already D syntax):
switch (x) {
case 1, 2, 3, 4:
doSomething();
break;
default:
whatever();
break;
}
C/C++ programmers that will want to use D will have to learn the new rule that
says: "In D there is no automatic case fallthrough". But allowing the
fallthrough when there are no statements after the label is a special case to
the general rule that they have to learn: ", unless there are no statements
inside the case.".
Special cases are bad in a programming language, they make manuals longer,
require more time and energy for the programmer to learn them, make the
compiler more complex and more buggy, and often slow down the programming even
whey they were created to give a handy special case.
-----------------
This D code gives a little more problems:
switch (x) {
case 1: .. case 10:
case 22: .. case 32:
case 52, 64:
doSomething();
break;
default:
whatever();
}
With the new proposal it can be translated like this (this is new syntax), that
considers a closed range of values one of the possible items of the list of
cases:
switch (x) {
case 1: .. case 10,
case 22: .. case 32,
52, 64:
doSomething();
break;
default:
whatever();
}
Or just like this (this is normal D syntax):
switch (x) {
case 1: .. case 10: goto case;
case 22: .. case 32: goto case;
case 52, 64:
doSomething();
break;
default:
whatever();
}
This shorter syntax (that uses a single 'case' statement) is now not available:
switch (x) {
case 1 ... 10, 22 ... 32, 52, 64:
doSomething();
break;
default:
whatever();
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349
Andrei Alexandrescu <andrei metalanguage.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |andrei metalanguage.com
06:30:56 PDT ---
This is all unnecessary aggravation. I don't think it's complicated for anyone
to understand that there's a requirement to insert a "break;" (or another
control flow statement) in places where a "break;" should be inserted anyway.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349 Answer to Comment 2: I agree that it's easy for people to understand that there's a requirement to insert a break or similar. This is what I have written in the Description. But this was not the point of my Comment 1. In Comment 1 I was talking about the *exception* to that easy rule. What I was trying to express is that I don't like such exception to the rule. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 21 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349 Walter has now vaguely accepted to require a control statements at the end of each case. But some people have asked for an exception to that rule, when a case is empty. This bug 4349 is against that exception. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 28 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349
Jonathan M Davis <jmdavisProg gmx.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jmdavisProg gmx.com
PDT ---
I think that the exception makes good sense and wouldn't mind having it but
that it's not particularly necessary. Thanks to ranged case statements and the
like, the need for multiple case statements which all fall through to a single
block of code is greatly reduced if not outright eliminated.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 28 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4349
Brad Roberts <braddr puremagic.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |braddr puremagic.com
---
This one is ready to be closed, right?
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 01 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4349
bearophile_hugs eml.cc changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |FIXED
This one is ready to be closed, right?
I don't like a lot the presence of a special case to the D rule that implicit
fall-through is disallowed in D (this code compiles):
void main() {
int x;
switch (x) {
case 1: /* x++; break; */
case 2: break;
default: break;
}
}
But I presume you are right, so I close this issue.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 02 2012









d-bugmail puremagic.com 