Loose ends: Optional

Ali Lahijani alahijani at gmail.com
Sun Jun 2 10:27:49 PDT 2013


On 5/30/13, Howard Lovatt <howard.lovatt at gmail.com> wrote:
> Why not return a List, or Set, of 1 or zero elements. No need for an extra
> class at all.
>
>  -- Howard.
>

I have been thinking of a variant of this idea for a while:
reinterpreting Optional as Collection of zero or one elements, a.k.a
Subsingleton. An interesting observation is that since there are no
more than one elements, this will be the only place where a class can
consistently implement both Set and List, and at the same time keep
with the letter of the spec.

class Optional<E> implements Set<E>, List<E>, RandomAccess { ...

Collections.{emptySet, emptyList, singletonSet, singleton} can all
delegate to Optional constructors, removing lots of code from
Collections. (The return types of these methods should not change to
preserve binary compatibility. I am not sure about Serialization
compatibility though.)

> On 29 May 2013 23:30, Remi Forax <forax at univ-mlv.fr> wrote:
>
>> On 05/29/2013 12:33 AM, Brian Goetz wrote:
>>
>>> OK, so the current proposal on the table is:
>>>
>>>  - Add filter, map, flatMap to Optional (these have a lot less utility
>>> on
>>> the primitive versions, plus map/flatMap would require more forms)
>>>  - Rename ifPresent to forEach to Optional and Optional{Int,Long,Double}
>>>
>>> Anyone who hasn't already expressed an opinion want to weigh in (or
>>> comment on the new aspects)?
>>>
>>
>> I think the two options are linked.
>> If ifPresent is renamed to forEach it means that Optional is seen as a
>> collection of elements (with zero or one element).
>>
>> Rémi
>>
>>
>>> On May 28, 2013, at 4:32 PM, Tim Peierls wrote:
>>>
>>>  Oh. Emily Litella.
>>>>
>>>>
>>>> On Tue, May 28, 2013 at 4:28 PM, Brian Goetz
>>>> <brian.goetz at oracle.com<mailto:
>>>> brian.goetz at oracle.com**>> wrote:
>>>>
>>>>     Sorry, Sam typo'ed and I missed it the first time.  He meant
>>>>     change "ifPresent(Consumer)" to "forEach(Consumer)".  Not
>>>> isPresent.
>>>>
>>>>     On May 28, 2013, at 4:26 PM, Tim Peierls wrote:
>>>>
>>>>      On Tue, May 28, 2013 at 4:22 PM, Brian Goetz
>>>>>     <brian.goetz at oracle.com <mailto:brian.goetz at oracle.com**>> wrote:
>>>>>
>>>>>         > The implementation decides if something can be lazy, I'm
>>>>>         ok with these having the same methods. In fact, I suggest we
>>>>>         change the name of isPresent to forEach.
>>>>>
>>>>>         I'm OK with this.  Its a little weird since there is at most
>>>>>         one element to each, but the connection to methods on other
>>>>>         containers is nice.
>>>>>
>>>>>
>>>>>     Please, no!
>>>>>
>>>>>     That would make Optional unusable for all the people who would
>>>>>     actually benefit from it.
>>>>>
>>>>>     There must be some serious disconnect. I thought this was all
>>>>>     settled.
>>>>>
>>>>>     --tim
>>>>>
>>>>
>>>>
>>>>
>>>
>>
>
>
> --
>   -- Howard.
>


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