expression switch vs. procedural switch
kevinb at google.com
Tue Mar 13 20:02:15 UTC 2018
I haven't been part of all the discussions that have led to this point, and
also am not a Language Person, so I apologize for the things I'm missing in
The more I have thought about it, the more I believe that 95% of the entire
value of expression switch is that it *isn't procedural switch*, and is
easier to reason about than procedural switch because of all things it *can't
- can't miss cases
- can't return
- can't break/continue a containing construct
- can't fall through
- (for constants or other disjoint patterns) can't depend on the order
As far as I can tell, its limitations are exactly what make it useful. (It
isn't really a large savings of code bulk, and it's not that we really want
to see switches appearing in expression contexts in general besides the key
ones `var = ??` and `return ??`.)
I also believe that all these limitations work to support the notion that
an expression switch is *functional* in nature. It is a function defined
"in parts" (and immediately executed). As such, I believe we should
discourage using expression switch in side-effecting ways. More to the
point, I suggest that side-effecting use cases should not be seen as
especially motivating for our design decisions (see e.g. my message 30
minutes ago in "break seen as a C archaism").
If we can manage to get in sync on the above (?), then I'd like to further
argue that we should *leave procedural switch alone* (with the *possible*
exception of enabling `case A, B` because that seems so very harmless, but
*meh*). I believe anything we do try to to smooth out differences between
procedural and expression switch and make them "really just the same thing"
directly works *against* the value proposition above. It can't be a simpler
thing if it is the same thing. And, of course, procedural switch has been
around and unchanged (except for new types) since the very beginning.
If we ever manage to agree on this much (?), then we can ask ourselves if
we should still use `switch` and `case` in expression switch or go with a
Again, sorry if I'm retreading ground that is already considered well trod.
Kevin Bourrillion | Java Librarian | Google, Inc. | kevinb at google.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the amber-spec-experts