-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
None
A DESCRIPTION OF THE PROBLEM :
Pre Java 11 using local variables with primitive types allowed the use of Optional map and orElse method to compute a condition and have a default fall-back value if the optional is absent.
Now with Java 11 if one infers the result of said computation it will be of type Boolean as orElse will return the boxed type and not a boolean.
My suggestion would be to extend Optional with
boolean mapToBooleanIfPresent(Predicate<T> predicate), boolean defaultValue);
where predicate will be used when the Optional is present and defaultValue will be used when the Optional is absent. This could internally also typecast the predicate result but more important the method will always return a primitive boolean therefore allowing user to use local variable type inference without explicit typecasting.
Pre Java 11 using local variables with primitive types allowed the use of Optional map and orElse method to compute a condition and have a default fall-back value if the optional is absent.
Now with Java 11 if one infers the result of said computation it will be of type Boolean as orElse will return the boxed type and not a boolean.
My suggestion would be to extend Optional with
boolean mapToBooleanIfPresent(Predicate<T> predicate), boolean defaultValue);
where predicate will be used when the Optional is present and defaultValue will be used when the Optional is absent. This could internally also typecast the predicate result but more important the method will always return a primitive boolean therefore allowing user to use local variable type inference without explicit typecasting.