Re:JDK 8 Release Notes
by hrjet on Wednesday March 19, 2014 @03:43AM
(#7427255)
It's not enough for compactly expressing imperative logic. In Scala:
Without pattern match:
The .get is the problem.
[ Parent
| on HN ]
Re:JDK 8 Release Notes
by benjiweber on Wednesday March 19, 2014 @05:28AM
(#7427479)
There are several ways of doing it concisely with the Java Optional
import java.util.Optional;
public class Scratch {
public static void main(String... args) {
Optional<String> foo = Optional.of("foo");
Optional<String> bar = Optional.empty();
System.out.println(foo.orElse("Error"));
System.out.println(bar.orElse("Error"));
foo.map(Print::print).orElseGet(() -> Print.print("Error"));
bar.map(Print::print).orElseGet(() -> Print.print("Error"));
foo.ifPresent(Print::print);
}
static class Print {
public static <T> T print(T val) {
System.out.println(val);
return val;
}
}
}