[swift-evolution] Add an ifPresent function to Optional

Thorsten Seitz tseitz42 at icloud.com
Mon Mar 14 01:54:59 CDT 2016


I agree that making Optionals conform to collection would be useful.

Nonetheless I think that ifPresent would be a useful addition because it makes the intent clearer if used on an Optional as opposed to a generic collection. Calling forEach is a little bit misleading if the static type is known to be an Optional, because it implies more that than one element might be present.

-Thorsten  

> Am 13.03.2016 um 18:04 schrieb Erica Sadun via swift-evolution <swift-evolution at swift.org>:
> 
> `forSome` suggests that there is a filter in-place selecting some but not all of a collection's members. `forEach` better reflects that a closure is applied to each member of a collection. I am in favor of making optionals consistently act like 1-item-max collections.
> 
> -- Erica
> 
> 
>> On Mar 13, 2016, at 10:58 AM, Pierre Monod-Broca via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Kevin: +1
>> 
>> I don't know if it's relevant to put it in the standard library, but about the name : forSome would make more sense to me than forEach, or ifPresent
>> 
>> Pierre
>> 
>>> Le 12 mars 2016 à 17:32, Kevin Lundberg via swift-evolution <swift-evolution at swift.org> a écrit :
>>> 
>>> 
>>>> +1 -- I've used the "an optional is a collection with max 1 element" rhetoric to explain map/flatMap, would be great if this was true instead of "morally true".
>>> 
>>> If optional were a sequence or collection, then could if let be considered a special case for this?:
>>> 
>>> for x in optionalX {
>>> //...
>>> }
>>> 
>>> I do like the idea that optional could be considered a collection, but I also worry about confusion in this case:
>>> 
>>> let a: [Int]? = [1,2,3]
>>> 
>>> for x in a {
>>> // a is [1,2,3], not the expected iteration of 1 through 3
>>> }
>>> 
>>> Today this does not compile and you are forced to unwrap the optional somehow. With this change I can see that someone may unexpectedly try this and be confused as to why the type of x is not what they expect. 
>>> 
>>>>> @Dave,Yes, CollectionType would be better than SequenceType.
>>>>> 
>>>>> On Saturday, 12 March 2016, Dave via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
>>>>>> +1.
>>>>>> 
>>>>>> I’d go so far as to suggest that maybe Optional should conform to CollectionProtocol, since you can pretty easily think of an Optional as a collection that can’t have more than one element.
>>>>>> 
>>>>>> - Dave Sweeris
>>>>>> 
>>>>>>> On Mar 10, 2016, at 7:32 PM, Erica Sadun via swift-evolution<swift-evolution at swift.org(javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');)>wrote:
>>>>>>> 
>>>>>>> While I'm not convinced of the utility or lack thereof, wouldn't it make more sense
>>>>>>> to extend `forEach` to support optionals in a similar way that map and flapMap
>>>>>>> currently do rather than introduce `ifPresent`?
>>>>>>> 
>>>>>>> lety:Int? =1
>>>>>>> letn:Int? =nil
>>>>>>> 
>>>>>>> _=y.map({print($0) }) // works in current Swift
>>>>>>> _=n.map({print($0) }) // ditto
>>>>>>> 
>>>>>>> 
>>>>>>> y.forEach{ print($0) } // not currently a thing
>>>>>>> n.forEach{ print($0) } // ditto
>>>>>>> 
>>>>>>> Just spitballing here.
>>>>>>> 
>>>>>>> -- E
>>>>>>> 
>>>>>>> _______________________________________________
>>>>>>> swift-evolution mailing list
>>>>>>> swift-evolution at swift.org(javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');)
>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>>> --
>>>>> -- Howard.
>>>> _______________________________________________
>>>> 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
>> _______________________________________________
>> 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