4 ms·
JEP325 seems like a nice little improvement. Just a linguistic trick but still lovely. int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY ->
by mabbo 8y ago
JEP325 seems like a nice little improvement. Just a linguistic trick but still lovely.
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
};
Switches can return values, and can have multiple cases on the same line.
I'm sure in 2 years when my organization gets around to switching to Java 12, I'll enjoy using it.
- maxxxxx 8y agoWhat happens if the value is not found? I would love it if you could throw an exception from a default block.
- hocuspocus 8y agoYou need a default clause when your cases aren't exhaustive otherwise. The compiler will insert a default clause transparently if you switch over an enum: https://openjdk.java.net/jeps/325 https://openjdk.java.net/jeps/325 The cases of a switch expression must be exhaustive; for any possible value there must be a matching switch label. In practice this normally means simply that a default clause is required; however, in the case of an enum switch expression that covers all known cases (and eventually, switch expressions over sealed types), a default clause can be inserted by the compiler that indicates that the enum definition has changed between compile-time and runtime. (This is what developers do by hand today, but having the compiler insert it is both less intrusive and likely to have a more descriptive error message than the ones written by hand.) This is pretty much the behavior you'd expect if you're familiar with languages that support pattern-matching.
- helfire 8y agoJava 12 is not a long term support release and will only be supported until September 2019 (so don't switch to it in 2 years :). Interesting to see the faster pace releases to get feedback on features, but many major libraries are just getting Java 11 support.
- cogman10 8y agoThis is mostly due to Java 9 breaking everything. Post Java 11, I doubt you will see the same problems you did with going from 8 to 9 or 11
- wbl 8y agoIntroduced for the first time in Algo 68 as far as I know.
- aardvark179 8y agoIt’s only a preview feature in 12, so you need to give a command line switch to java for it to be enabled. It’s also the first part of a much larger feature adding pattern matching to the Java language. Getting all the ducks in a row for this sort of thing is tricky because it touches so many other ares, but switch expressions can be delivered on their own quite nicely. Adding patterns means looking at variable scopes (where is a variable bound in a pattern in scope?), record classes (because destructuring patterns should not be implemented by hand everywhere), and even constants and raw string literals (because being able to switch on regexps would be nice, and you need a good way to write that).
- mabbo 8y agoThat sounds lovely. Fully functional pattern matching in Java would be a godsend.