[swift-evolution] [Review] SE-0187: Introduce Sequence.filterMap(_:)

Kevin Ballard kevin at sb.org
Wed Nov 8 14:07:06 CST 2017


On Wed, Nov 8, 2017, at 11:28 AM, Max Moiseev wrote:
> 
> 
>> On Nov 7, 2017, at 3:34 PM, Kevin Ballard via swift-evolution <swift-
>> evolution at swift.org> wrote:>> 
>> On Tue, Nov 7, 2017, at 03:23 PM, John McCall via swift-
>> evolution wrote:>>>> https://github.com/apple/swift-evolution/blob/master/proposals/0187-introduce-filtermap.md>>> 
>>> • What is your evaluation of the proposal?
>> 
>> This proposal is going to cause an insane amount of code churn. The
>> proposal suggests this overload of flatMap is used "in certain
>> circumstances", but in my experience it's more like 99% of all
>> flatMaps on sequences are to deal with optionals, not to flatten
>> nested sequences.>> 
>>> • Is the problem being addressed significant enough to warrant a
>>> change to Swift?>> 
>> I don't think so. It's a fairly minor issue, one that really only
>> affects new Swift programmers anyway rather than all users, and it
>> will cause far too much code churn to be worthwhile.>> 
>> I'd much rather see a proposal to add a new @available type,
>> something like 'warning’,> Please write one, seriously!
> 
>> that lets you attach an arbitrary warning message to a call (which
>> you can kind of do with 'deprecated' except that makes the warning
>> message claim the API is deprecated). With that sort of thing we
>> could then declare>> 
>> extension Sequence {
>>     @available(*, warning: "Use map instead")
>>     func flatMap<U>(_ f: (Element) -> U) -> [U] {
>>         return map(f)
>>     }
>> }
> FWIW: This was attempted in the past, and had to be reverted for the
> reason other than «deprecated» being a confusing message.> https://github.com/apple/swift/pull/9390

Interesting. It looks like what's going on is, with the deprecated form,
`[a, b, c]` is immediately treated as `[Any]`, but without it, it's
treated as `[Int?]` (invoking optional hoisting on `a`) and the closure
itself is inferred as returning Any.
There's probably some shenanigans we could do like adding an
_ImplicitlyPromotedOptional<T> type that takes precedence over
Optional<T> for overloading purposes, which could then be used to
reintroduce the deprecated form of flatMap, but I'm not sure if it's
worth the additional complexity unless there are more functions than
just flatMap that we'd want to kill optional hoisting for.
-Kevin Ballard

> 
>> 
>> And now if someone writes flatMap in a way that invokes optional
>> hoisting, it'll match this overload instead and warn them.>> 
>>> • How much effort did you put into your review? A glance, a quick
>>> reading, or an in-depth study?>> 
>> A quick reading, and a couple of minutes testing overload behavior
>> with availability attributes (to confirm that we can't simply use
>> 'unavailable' for this).>> 
>> -Kevin Ballard
>> 
>> _______________________________________________
>> 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/20171108/46d594d7/attachment.html>


More information about the swift-evolution mailing list