[swift-evolution] [Review] SE-0187: Introduce Sequence.filterMap(_:)
Tino Heth
2th at gmx.de
Wed Nov 8 12:34:20 CST 2017
> Am 08.11.2017 um 19:23 schrieb Kevin Ballard via swift-evolution <swift-evolution at swift.org>:
>
> On Tue, Nov 7, 2017, at 05:23 PM, Tino Heth via swift-evolution wrote:
>> -1
>>
>> I guess breaking existing code will be the show stopper for this proposal — but I generally think that compatibility is a poor rationale to stop an improvement, so my personal reasons are different:
>> The name is just wrong.
>> Just have a look at this simple example
>>
>> extension Int {
>> func justImagineError() throws -> Int {
>> return self
>> }
>> }
>>
>> let ints: [Int?] = [nil]
>>
>> let result = ints.flatMap {
>> return try? $0?.justImagineError()
>> }
>> print(result)
>>
>> If flatMap would really filter out nil values, this should yield an empty array as result — but the actual output is [nil]
>
> flatMap does filter out nil values. The problem is the return type of your block is `Int??`, not `Int?`, so it's stripping off the outer layer of optionals.
The filter function afaik doesn’t perform that kind of stripping - if you feed it [A?], you get [A?] as output, no matter what kind of predicate you use, it can only eliminate values (Optional.none), not change their type.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171108/99409d7a/attachment.html>
More information about the swift-evolution
mailing list