[swift-evolution] Add an ifPresent function to Optional

Ilya Belenkiy ilya.belenkiy at gmail.com
Sun Mar 20 22:36:41 CDT 2016


> * Are the current stdlib names for optional map and flatMap misleading? 


It depends on the interpretation. The one that I came across in “Functional Swift” describes map and flatMap for container types that are not necessarily collections (Result is another container type that immediately comes to mind which is not a collection). Since these concepts are not specific to Swift, I think that it’s very important that Swift uses the names that are already established for these concepts.

> * Are the current stdlib functions for optional closure application appropriate and sufficient?

I’d love to see what is called “ifPresent” in the subject of the thread. But I think that it should be called “apply” (https://en.wikipedia.org/wiki/Apply)

> On Mar 20, 2016, at 1:52 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Questions being raised in this discussion:
> 
> * Are the current stdlib names for optional map and flatMap misleading? 
> * Are the current stdlib functions for optional closure application appropriate and sufficient?
> 
>     @warn_unused_result
>     public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U?
> 
>     @warn_unused_result
>     public func flatMap<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U?
> 
> Assume there could be up to three stdlib functions just for applying closures to .some-case 
> optionals. Would they look like this?
> 
> public func f1<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U?
> public func f2<U>(@noescape f: (Wrapped) throws -> U!) rethrows -> U!
> public func f3<U>(@noescape f: (Wrapped) throws -> U) rethrows -> Void
> 
> Right now f2/flatMap returns U?, not U or U!, and won't throw if nil, right? Should it
> stay that way or change to something that throws and returns a guaranteed value?
> Or maybe the current "f2" model (flatMap)  be discarded and replaced by an 
> "ifPresent"-like Void call?
> 
> And regardless of which functions are included, what would the appropriate 
> names for each function style be?
> 
> -- E
> 
>> On Mar 20, 2016, at 11:22 AM, Andrey Tarantsov <andrey at tarantsov.com <mailto:andrey at tarantsov.com>> wrote:
>> 
>>> No.  My argument is that map on collections and on optionals are two
>>> completely different things.  They unify on a level that does not
>>> exist in Swift (higher-kinded types).
>> 
>> +1000.
>> 
>> Optional.map is already highly unfortunate. It makes optional arrays especially painful to deal with, but even in general, you can no longer glance at the code and see which parts are dealing with many items and which parts are dealing with single items.
>> 
>> I would definitely support renaming Optional.{map,flatMap}.
>> 
>> A.
>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160320/c38eaecc/attachment.html>


More information about the swift-evolution mailing list