3 ms·
int val = switch (str) { case "foo" -> 1; case "bar" -> 2; case "baz" -> 3; default -> -1; } Looks a lot like Scala: val foo =
by virtualwhys 8y ago
int val = switch (str) {
case "foo" -> 1;
case "bar" -> 2;
case "baz" -> 3;
default -> -1;
}
Looks a lot like Scala:
val foo = match (str) {
case "foo" => 1
case "bar" => 2
case "baz" => 3
case _ => -1
}
Interestingly the underscore character is now reserved in Java, wonder what use case(s) they have in mind there?
- kovrik 8y agoBrian Goetz said: > We are "reclaiming" the syntactic real estate of "_" from the space of identifiers for use in future language features. However, because there are existing programs that might use it, it is a warning for identifiers that occur in existing syntactic positions for 8, and an error for lambda formals (since there is no existing code with lambdas.)
- sk5t 8y agoScala's pattern matching (which btw is structured "value match { ... }" - think of .match as an always-present extension method) goes much farther than it is easy to imagine Java going quickly; consider examples such as regexp capture decomposition, case classes, static type checking, and guards.
- krzyk 8y agoRegarding "_": http://openjdk.java.net/jeps/302 http://openjdk.java.net/jeps/302 Example: BiFunction<Integer, String, String> biss = (i, _) -> String.valueOf(i); It basically means that one of the lambda parameters is unused. And citing this JEP 302: We are now free to complete the planned rehabilitation of underscore to indicate an unused lambda, method, or catch formal parameter.
- virtualwhys 8y agoScala overloads underscore in various ways, my favorite being: val emails = users.map(_.email) Unlikely that will make its way into Java anytime soon, if ever, but it would be a nice enhancement -- being forced to pepper your code with lambda expressions a la `x => x.foo` is annoying when the alternative `_.foo` conveys the same meaning.