0

I'd like to understand why Swift 4 and Swift 3.2 have different flatMap behaviors.

Consider the following code:

let myURLArray: ([URL]) = [ URL(string: "https://www.google.fr")! ]
let result = myURLArray.flatMap { $0.absoluteString }
print(result)

With Swift 3.2:

result is interpreted as a [String] and the output is:

["https://www.google.fr"]

With Swift 4:

result is interpreted as a [String.Element] and the output is:

["h", "t", "t", "p", "s", ":", "/", "/", "w", "w", "w", ".", "g", "o", "o", "g", "l", "e", ".", "f", "r"]

I cannot find the item describing this change of behavior in Swift 4's release notes. Can anybody explain?

Mick F
  • 7,312
  • 6
  • 51
  • 98
  • 1
    And the solution is to replace `flatMap` by `map` (since `absoluteString` does not return an optional). That should behave identically in both Swift versions. – Martin R Jan 22 '18 at 14:11
  • 1
    In `Swift` **2-3** `String` **is not** `Collection`, in `Swift` **1, 4** it [**is** `Collection`](https://github.com/apple/swift-evolution/blob/master/proposals/0163-string-revision-1.md). And `flatMap` "lifts" collection's content one level up. – user28434'mstep Jan 22 '18 at 14:13
  • 1
    With respect to the Swift 4 release notes: This is a consequence of SE-0163. – Martin R Jan 22 '18 at 14:13

0 Answers0