hg: lambda/lambda/jdk: Cleanup in Optional

Gernot Neppert mcnepp02 at googlemail.com
Fri Nov 23 04:09:38 PST 2012


I don't think it's realistic to be "thinking you do not have null and you'd
rather have an exception".
Either I know the value is null, then I may call empty(), or I don't know,
in which case I wouldn't want to invoke a method that might throw an
Exception. I'd rather get a valid 'Optional' instance, be it empty or not.

Note that everything would be different if 'Optional' were allowed to have
'null' values. Then of course we'd need to be able to discriminate between
'No value at all' and 'The value null'. But since that has been voted down,
the reason for having distinct factory methods has (IMO) gone away.


> You might be calling .of(T) thinking you do not have null and you'd
rather have an exception than an empty Optional as the result.


2012/11/23 Ricky Clarkson <ricky.clarkson at gmail.com>

> That's a useful method but I'd suggest calling it 'option' or
> 'fromNullable'.  You might be calling .of(T) thinking you do not have null
> and you'd rather have an exception than an empty Optional as the result.
>  For reference, Scala calls it option, Guava calls it fromNullable,
> Functional Java calls it fromNull and all 3 make that a distinct method
> from the one that throws an exception if it's null (Some(t) in Scala, of in
> Guava, some(t) in Functional Java).
>
>
> On Fri, Nov 23, 2012 at 5:36 AM, Gernot Neppert <mcnepp02 at googlemail.com>wrote:
>
>> Hi Brian,
>>
>> I do think that having factory methods 'empy()' and 'of(T)' and no public
>> constructor is a good thing, I wonder whether it's necessary to make
>> 'of(T)' is so restrictive: it throws NullPointerException if the supplied
>> value is null.
>> So why place the burdon of picking the correct factory method upon the
>> user?
>> Why not simply have:
>>
>> public static <T> Optional<T> of(T value) {
>>   return value == null ? empty() : new Optional<T>(value);
>> }
>>
>>
>>
>>
>> 2012/11/15 <brian.goetz at oracle.com>
>>
>> > Changeset: ab258565c0c9
>> > Author:    briangoetz
>> > Date:      2012-11-15 17:19 -0500
>> > URL:
>> http://hg.openjdk.java.net/lambda/lambda/jdk/rev/ab258565c0c9
>> >
>> > Cleanup in Optional
>> >
>> > ! src/share/classes/java/util/Optional.java
>> > ! src/share/classes/java/util/streams/ops/FindAnyOp.java
>> > ! src/share/classes/java/util/streams/ops/FindFirstOp.java
>> > ! src/share/classes/java/util/streams/ops/SeedlessFoldOp.java
>> >
>> >
>> >
>>
>>
>


More information about the lambda-dev mailing list