[swift-evolution] Add an ifPresent function to Optional
Ilya Belenkiy
ilya.belenkiy at gmail.com
Sun Mar 20 22:09:28 CDT 2016
It’s a matter of interpretation:
map on a container type extracts the value from the container, applies the function, then puts the result in the container. The container doesn’t need to be a collection. Same for flatMap.
I think that a function like this would be useful, but I’d call it “apply”. Also, I think that the fact Optional is not a collection type is a good thing. While it can be treated as a collection of at most 1 element, semantically it’s not. We don’t really iterate over all elements in an optional value.
> On Mar 14, 2016, at 4:28 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On Mar 14, 2016, at 2:18 PM, Daniel Vollmer via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Hi,
>>
>>> On 13 Mar 2016, at 18:04, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> I am in favour of making optionals consistently act like 1-item-max collections.
>>
>> Why stop there? Why not make all non-optionals collections of one?
>
> I don't quite see what advantage would that have. Could you elaborate?
>
> There's precedent in map and flatMap for treating optionals this way.
>
>>
>> I’m not convinced that making optionals conform to CollectionType actually improves matters: Yes, it may make the odd use here or there slightly more convenient, but at the cost of clarity IMO: Optionals have largely different set of concerns than collections, and having them suddenly inherit all CollectionType functionality and extensions doesn’t make their use (if it relies on them) any clearer, it obscures their “Optional”-ness instead.
>>
>> Daniel.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
More information about the swift-evolution
mailing list