Skip to content

Commit 9d70574

Browse files
author
Max Moiseev
committed
[stdlib] Remove the unnecessary compactMap overload
This particular overload of compactMap was mechanically copied from the `flatMap` while implementing SE-0187, but as community members correctly noticed on the forum thread [here](https://forums.swift.org/t/why-is-there-a-collection-flatmap-that-takes-a-closure-returning-string/10141), there is no code that we should be backward compatible with, since `compactMap` has only been introduced recently. After applying this change, the following code is correctly ambiguous again: ```swift [].compactMap { _ in nil } // error: 'nil' requires a contextual type ``` as opposed to: ```swift [].flatMap { _ in nil } // r0 : [String] = [] ``` Fixes: https://bugs.swift.org/browse/SR-7052
1 parent 1785855 commit 9d70574

File tree

1 file changed

+0
-35
lines changed

1 file changed

+0
-35
lines changed

stdlib/public/core/StringRangeReplaceableCollection.swift.gyb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -574,41 +574,6 @@ extension Sequence {
574574
}
575575

576576
extension Collection {
577-
/// Returns an array containing the non-`nil` strings resulting from the given
578-
/// transformation on each element of this sequence.
579-
///
580-
/// Use this method to receive an array of non-optional strings when your
581-
/// transformation produces a `String?`.
582-
///
583-
/// In this example, note the difference in the result of using `map` and
584-
/// `compactMap` with a transformation that returns a `String?` value.
585-
///
586-
/// let errorLookup = [400: "Bad request",
587-
/// 403: "Forbidden",
588-
/// 404: "Not found"]
589-
///
590-
/// let errorCodes = [400, 407, 404]
591-
///
592-
/// let mapped: [String?] = errorCodes.map { code in errorLookup[code] }
593-
/// // ["Bad request", nil, "Not found"]
594-
///
595-
/// let compactMapped: [String] = errorCodes.compactMap { code in errorLookup[code] }
596-
/// // ["Bad request", "Not found"]
597-
///
598-
/// - Parameter transform: A closure that accepts an element of this
599-
/// sequence as its argument and returns a `String?`.
600-
/// - Returns: An array of the non-`nil` results of calling `transform`
601-
/// with each element of the sequence.
602-
///
603-
/// - Complexity: O(*m* + *n*), where *m* is the length of this sequence
604-
/// and *n* is the length of the result.
605-
@_inlineable // FIXME(sil-serialize-all)
606-
public func compactMap(
607-
_ transform: (Element) throws -> String?
608-
) rethrows -> [String] {
609-
return try _compactMap(transform)
610-
}
611-
612577
@available(swift, deprecated: 4.1, renamed: "compactMap(_:)",
613578
message: "Please use compactMap(_:) for the case where closure returns an optional value")
614579
@inline(__always)

0 commit comments

Comments
 (0)