1

java.util.Optional.or, orElse and orElseGet should really have their signatures changed so they can widen the type. For example, `Optional<T> or​(Supplier<? extends Optional<? extends T>> supplier)` should really be `<U, S super T & U> Optional<S> or​(Supplier<? extends Optional<? extends U>> supplier)`

Then we could eliminate the following unchecked cast:
```
public static double getOrNan(Optional<? extends Number> optionalNumber) {
return ((Optional<Number>)optional).getOrElse(Double.NaN).doubleValue();
}
```

Comments
  • 1
    hmm

    public static double getOrNan(Optional<? extends Number> optionalNumber) {
    return optionalNumber.map(Number::doubleValue).orElse(Double.NaN);
    }
  • 0
    @tkdmatze That works, but it's more unboxing and re-boxing of the double. How much of that can the C2 compiler optimize out?
Add Comment