Optional require(s) NonNull
Brian Goetz
brian.goetz at oracle.com
Mon Oct 29 10:50:32 PDT 2012
Take a look at the code today :)
On Oct 29, 2012, at 12:33 PM, Boaz Nahum wrote:
> I can only blame my poor English.
>
> Lets forget the 'philosophic' first part of my question.
>
> the second part asks: According to current implementation in b62,
> 'this.present' is false i.f.f 'this.value' is null --> 'this.present' ==
> (this.value != null ). So why we need 'this.present' ?
>
> If you allow me insist of the first part - Why won't allow me to create a
> *present* Optional with null value ?
>
> On Mon, Oct 29, 2012 at 5:15 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>
>> This constructor is for creating a *present* Optional. You use
>> Optional.empty() to create an *empty* optional.
>>
>> On Oct 29, 2012, at 11:02 AM, Boaz Nahum wrote:
>>
>>> I thought I understand Optional till I read the implementation:
>>>
>>> public Optional(T value) {
>>> this.value = Objects.requireNonNull(value);
>>> this.present = true;
>>> }
>>>
>>> Why 'requireNonNull' non null ? If null is singular value why we need
>> this
>>> class ? just for 'orElse..' methods ?
>>>
>>> If if value can't be null why we need the 'present' field ?
>>>
>>> My hopes where that I can use Optional to return a null as a legal value.
>>>
>>> Thanks
>>> Boaz
>>>
>>
>>
>
More information about the lambda-dev
mailing list