JDK-8214753 : (opt) add Optional::transform, allowing an arbitrary operation on an Optional
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2018-12-04
  • Updated: 2024-05-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Description
Add a method something like this to Optional:

    public <R> R transform(Function<Optional<T>, ? extends R> f) {
        return f.apply(this);
    }

This allows the caller to provide an arbitrary operation to be performed on "this" Optional, regardless of whether it's empty or present. The name "transform" follows precedent set by String::transform (JDK-8203442).
Comments
Applying the PECS or get/put rule would seem to indicate that the signature should be <R> R transform(Function<Optional<? super T>, ? extends R> f) but this doesn't actually have the desired effect of allowing a function that accepts a supertype of Optional<T>. Suggestions welcome.
04-12-2018