[Pitch] Deprecate `Optional.map(_:)`

Yep! It’s easier to see how map and flatMap are consistent when you look at their shapes:

// map
[A]  => ((A) ->   B ) ->  [B]
 A?  => ((A) ->   B ) ->   B?
F<A> => ((A) ->   B ) -> F<B>

// flatMap
[A]  => ((A) ->  [B]) ->  [B]
 A?  => ((A) ->   B?) ->   B?
M<A> => ((A) -> M<B>) -> M<B>

Meanwhile, compactMap is more concrete and complicated, and uses both Optional and Array (or Sequence):

[A?] => ((A?) -> B?) -> [B]

These abstractions aren’t easy to learn (at least they weren’t for me), and a couple design choices Swift has made make them all the more difficult: both automatic optional promotion and our previously overloaded flatMap (soon compactMap) make these functions behave in surprising ways.

12 Likes

I think @stephencelis described the correctness of existing names already, but let me also add that I'm pretty sure map and flatMap fall into term-of-art category of Swift API design.

As for the pitch, I'm against it. Both Optional.map and Optional.flatMap have their uses today and I can't imagine them disappearing.

Also, I'd like to stress this:

4 Likes

I stand corrected. Thanks to @stephencelis for the great explanation of the difference between the signatures of the two functions. At this stage of Swift (and this stage of Swift Evolution, as noted by @xwu), it seems that both functions are still important.

2 Likes

What issues do you have with this proposal?