switch()

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 16 23:45:14 PST 2014


On Monday, February 17, 2014 01:42:59 Manu wrote:
> So D offers great improvements to switch(), but there are a few small
> things I wonder about.
> 
> 1.
> case fall-through is not supported; explicit 'goto case n;' is required.
> With this in mind, 'break' is unnecessary. Why is it required? It could be
> implicit upon reaching the next case label, or a scope could be used (with
> support for omitting the scope for single statements as with if).
> It's really noisy, and annoying to write everywhere.

That goes against the D philosophy of making it so that C code is either valid 
D code with the same semantics, or it doesn't compile, so it's arguably not an 
option, much as it would make switch statements much less noisy. We _have_ in 
a very few cases broken the rule about C compatibility (e.g. static arrays are 
passed by value rather than reference), but it's not something that we've done 
much. And in this case, it could be particular pernicious, because any C/C++ 
code being ported over would end up with the reverse of its current semantics 
in any case that doesn't end with break. The same goes for any older D code 
that hasn't been compiled since implicit fallthrough became illegal. Compiling 
it with the newer compiler which had implicit breaks would silently alter its 
semantics.

So, while in principle, I like the idea, I don't think that it's actually a 
viable one given D's design goals and history.

- Jonathan M Davis


More information about the Digitalmars-d mailing list