Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8269520

Primitive boolean shortcut for Optional

XMLWordPrintable

      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.


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: