Where has the map method on Optional moved?

Paul Sandoz paul.sandoz at oracle.com
Tue Feb 26 02:47:25 PST 2013


On Feb 26, 2013, at 6:16 AM, Sam Pullara <sam at sampullara.com> wrote:
> I've never been comfortable with this. I'm glad Jed is calling it out.
> Can we make Optional first class or remove it?

Trawling through the email archives i cannot find specific discussion on Optional.map/flatMap, anyone recall such discussion? There is some general discussion on not going down the route of an option monad.

Since we have added a Stream.flatMap method following the pattern expected of it, does that give more weight to argument of adding such a method to Optional, perhaps of the same or a different name to disassociate with Stream itself?

I find myself more and more leaning towards the position of if we include an Option class some developers will expect bind functionality, it seems useful, and nor is it hard to explain its use while avoiding mention the "m" word.

Paul.


> 
> Sam
> 
> On Mon, Feb 25, 2013 at 9:12 PM, Jed Wesley-Smith <jed at wesleysmith.io> wrote:
>> Hi Paul,
>> 
>> You don't get a choice, it is a (or forms a) monad, you just removed
>> the useful methods (map/flatMap aka fmap/bind). This leaves clients to
>> implement them (or the functionality) in an ad-hoc and possibly buggy
>> form themselves.
>> 
>> It is a monad if there exists some pair of functions:
>> 
>> A -> Option<A>
>> Option<A> -> (A -> Option<B>) -> Option<B>
>> 
>> The first is Optional.of, the second is currently:
>> 
>> Optional<A> a = …
>> Optional<B> b = …
>> Function<A, Optional<B> f = …
>> if (a.isPresent) {
>>  b = f.apply(a.get());
>> } else {
>>  b = Optional.empty();
>> }
>> 
>> rather than:
>> 
>> Optional<A> a = …
>> Function<A, Optional<B> f = …
>> final Optional<B> b = a.flatMap(f);
>> 
>> cheers,
>> jed.
>> 
>> On 26 February 2013 00:12, Paul Sandoz <paul.sandoz at oracle.com> wrote:
>>> Hi Dhananjay,
>>> 
>>> It is not missing it was removed.
>>> 
>>> java.util.Optional has a narrower scope that optional things in other languages. We are not trying to shoe-horn in an option monad.
>>> 
>>> Paul.
>>> 
>>> On Feb 23, 2013, at 12:27 AM, Dhananjay Nene <dhananjay.nene at gmail.com> wrote:
>>> 
>>>> It seemed to be there on the Optional class in b61 but is missing now. Is
>>>> there some way to run map/flatMap operations on an Optional?
>>>> 
>>>> Thanks
>>>> Dhananjay
>>>> 
>>> 
>>> 
>> 



More information about the lambda-libs-spec-observers mailing list