[swift-evolution] Add an ifPresent function to Optional

Andrey Tarantsov andrey at tarantsov.com
Wed Mar 23 16:36:40 CDT 2016


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

Not so much misleading, as being unfortunate when optionals and collections end up as neighbors in the code.


> * Are the current stdlib functions for optional closure application appropriate and sufficient?
> 
> 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

I don't see why flatMap needs to be separate from map. It just does not make sense. I would only have

public func f2<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U?

and would call it "ifPresent", "then", "unwrap", "transform" or something like that.

A.

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


More information about the swift-evolution mailing list