RFE: add missing methods to Optional, OptionalInt, OptionalLong and OptionalDouble

Brian Goetz brian.goetz at oracle.com
Tue Feb 19 16:01:26 UTC 2019


This is a very nice patch, complete with spec and tests, and evidence of 
OCA.

But, before we talk about patches and code, let's step back and talk 
about stewardship.  With API proposals, we like to start with problems, 
rather than solutions: what problems we are concerned with, and are 
these are problems that need to be solved in the JDK?  Assuming we like 
the answers to the former, we can then move on to whether the proposed 
solution is the right one, and then to details like spec and code review.

We are currently trying to avoid adding anything to 
OptionalInt/Long/Double, because our current plan is to replace these 
with a specialized Optional<int> once Valhalla delivers.  Any methods 
that differ from the base type (Optional) creates potential roadblocks 
to such a migration, so we'd prefer to leaves these classes be.

To the main question, though, whether Optional should have specialized 
map and flatMap methods ... I'm skeptical.  The goal was never to create 
parity between Optional and Stream, and my gut reaction is that the 
return on additional API surface area here is negative.  But, we can 
surely have a discussion on this.



On 2/16/2019 7:52 AM, Rob Spoor wrote:
> I noticed that, while Stream and its primitive equivalents have 
> multiple map and flapMap methods, Optional and its primitive 
> equivalents were missing those. Since these can still be very useful, 
> I wrote a patch to add the following methods:
>
> * Optional: mapToInt, mapToLong, mapToDouble, flatMapToInt, 
> flatMapToLong, flatMapToDouble
> * OptionalInt: map, mapToObj, mapToLong, mapToDouble, flatMap
> * OptionalLong: map, mapToObj, mapToInt, mapToDouble, flatMap
> * OptionalDouble: map, mapToObj, mapToInt, mapToLong, flatMap
>
> I did not add any more flatMap methods to OptionalInt, OptionalLong 
> and OptionalDouble because these also don't exist on IntStream, 
> LongStream and DoubleStream.
>
>
> In addition, I also added the missing or method to OptionalInt, 
> OptionalLong and OptionalDouble based on that of Optional.
>
>
> My OCA has been processed on 2019-01-22.
>
>
> Rob



More information about the core-libs-dev mailing list