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

(opt) add methods to convert to and from primitive Optional classes

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd_major
    • Component/s: core-libs
    • Labels:

      Description

      It's fairly difficult to convert between Optional<Integer> and OptionalInt and vice-versa. This also applies to the Long and Double specializations. The workaround is to use streams, but this is quite roundabout.

        Activity

        Hide
        smarks Stuart Marks added a comment -
        Mentioned by Rémi in http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-December/050477.html --

        ==========
        It seems that are no simple way to transform an Optional<Integer> to an OptionalInt and vice-versa.

        It's painful because we start to see interface that returns by example OptionalInt while the implementation you want to connect with return an Optional<Integer>.

        The only workaround seems to be to use a Stream/IntStream:
         Optional -> OptionalInt
           optional.stream().mapToInt(x -> x).findFirst()
         OptionalInt -> Optional
           optionalInt.stream().boxed().findFirst();

        I think, Optional should have the method mapTo*/flatMapTo* and Optional[Primitive] the method boxed().
        ==========
        Show
        smarks Stuart Marks added a comment - Mentioned by Rémi in http://mail.openjdk.java.net/pipermail/core-libs-dev/2017-December/050477.html -- ========== It seems that are no simple way to transform an Optional<Integer> to an OptionalInt and vice-versa. It's painful because we start to see interface that returns by example OptionalInt while the implementation you want to connect with return an Optional<Integer>. The only workaround seems to be to use a Stream/IntStream:  Optional -> OptionalInt    optional.stream().mapToInt(x -> x).findFirst()  OptionalInt -> Optional    optionalInt.stream().boxed().findFirst(); I think, Optional should have the method mapTo*/flatMapTo* and Optional[Primitive] the method boxed(). ==========
        Hide
        smarks Stuart Marks added a comment -
        The request is reasonable. The "obvious" thing to do is to add mapToInt/mapToObj/flatMapToInt/flatMapToObj (and corresponding for Long and Double) but doing the "obvious" thing isn't always the right thing. It seems like there should be an alternative way to do this without having to introduce a whole new family of APIs to deal with specializations. Further thought is called for here.
        Show
        smarks Stuart Marks added a comment - The request is reasonable. The "obvious" thing to do is to add mapToInt/mapToObj/flatMapToInt/flatMapToObj (and corresponding for Long and Double) but doing the "obvious" thing isn't always the right thing. It seems like there should be an alternative way to do this without having to introduce a whole new family of APIs to deal with specializations. Further thought is called for here.
        Hide
        forax Rémi Forax added a comment -
        I'm not sure there is a "right" thing to do here.
        We can wait until we have generics over primitive types but that's at least 2019 if we are optimistic, and even with generics over primitive types it's easy to do an optional.map(x -> map.get(x)) and forget that even if map() is parameterized with a primitive, the return value of map.get() can be a wrapper, optional.mapToInt(x -> map.get(x)) is more explicit.

        What we can do to limit the number of methods is just to add mapTo/flatMapTo to Optional and not the primitive specialization OptionalInt/OptionalDouble/OptionalLong and just add a method boxed() to those specializations. So only support box/unbox conversions and not primitive narrowing/widening conversions.
        Show
        forax Rémi Forax added a comment - I'm not sure there is a "right" thing to do here. We can wait until we have generics over primitive types but that's at least 2019 if we are optimistic, and even with generics over primitive types it's easy to do an optional.map(x -> map.get(x)) and forget that even if map() is parameterized with a primitive, the return value of map.get() can be a wrapper, optional.mapToInt(x -> map.get(x)) is more explicit. What we can do to limit the number of methods is just to add mapTo/flatMapTo to Optional and not the primitive specialization OptionalInt/OptionalDouble/OptionalLong and just add a method boxed() to those specializations. So only support box/unbox conversions and not primitive narrowing/widening conversions.

          People

          • Assignee:
            smarks Stuart Marks
            Reporter:
            smarks Stuart Marks
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: