[swift-evolution] Add an ifPresent function to Optional

Krzysztof Siejkowski krzysztof at siejkowski.net
Sun Mar 20 13:20:45 CDT 2016


Great discussion!

Questions being raised in this discussion:

* Are the current stdlib names for optional map and flatMap misleading? 
I believe they could be. 

From what I could read, the core team and a majority of community doesn’t believe that including the abstractions like Functor or Monad into the stdlib is a good idea. Which is ok - we have a number of great 3rd party implementations avaliable, e.g. Swiftz. Scala took the same route.

If we’re not trying to go into Haskell land, than keeping names like map and flatMap for Optional is just making it harder for the developers who are not familiar with the functional programming jargon. At the same time, those accustomed to functional programming will grab the concept very easily regardless of what it’s called. The name already vary significantly in various languages (see https://en.wikipedia.org/wiki/Map_(higher-order_function)#Language_comparison ).



* Are the current stdlib functions for optional closure application appropriate and sufficient?
I think not. Again, if we’re not pushing people into “don’t leave the monad” mindset than it’s a very good idea to provide a standalone method for side effects. Erica wrote the signature:

public func f3<U>(@noescape f: (Wrapped) throws -> U) rethrows -> Void


However, I’m not sure about this one:

public func f2<U>(@noescape f: (Wrapped) throws -> U!) rethrows -> U!
I haven’t used implicitly unwrapped optionals at all, I just never found a good case for them. Why do you think this variant might be useful?



- Krzysztof



-- E

On Mar 20, 2016, at 11:22 AM, Andrey Tarantsov <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/0191acd2/attachment-0001.html>


More information about the swift-evolution mailing list