Switch and break
Jonathan M Davis
jmdavisProg at gmx.com
Thu Jan 19 16:50:57 PST 2012
On Friday, January 20, 2012 00:38:49 Era Scarecrow wrote:
> Personal experience in a program (not a language) is that having certain
> options on automatically usually is more of an annoyance, than manually
> turning them on. With that said, having explicit fall through sounds more
> useful than explicit breaking. However people coming form C and C++, the
> switch statement changes would be enough to cause their own bugs and
> perhaps discouragement.
The general rule with D is that C/C++ code must either compile as D code and
have the same semantics, or it must not compile as D code. There are a few
places where that isn't true (e.g. parameters which are static arrays are
passed as values in D but not C), but it's fairly rare. So, aside from the
fact that it would break lots of D code, making it so that fallthrough was
explicit and breaking wasn't wouldn't be acceptable. Not to mention, most
programmers would be confused by D's switch if it worked that way, since most
languages went with implicit fallthrough (presumably because that's what C
did). So, making it explicit for both seems like a good way to go.
> The true use in fallthrough, would be if you wanted more than one option to
> trigger a specific block of code. An example could be with flags, Unix
> flags have somewhat of a standard, so a program that defined it's own flags
> may later add secondary flags for compatibility purposes.
>
> switch(i) {
> case 2:
> case 3:
> case 5:
> case 7:
> printf("%d is a prime between 1 and 10", i);
> break;
> default:
> printf("%d is not a prime between 1 and 10", i);
> }
That actually still triggers implicit fallthrough. -w disallows implicit
fallthrough only in cases where a case statement has code in it.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list