Files
swift-mirror/test/stdlib/StringFlatMap.swift
Maxim Moiseev 31f7dfe475 [stdlib] Backward compatibility fix for a flatMap on [String] (#9466)
* [stdlib] Backward compatibility fix for a flatMap on [String]

Since String started to conform to Collection, the flatMap with a
sequence returning closure is now a better match that the one that
relies on the optional promotion in this code:

[""].flatMap { $0 }

which results in the default type of this expression changing from
[String] to [Character].

Restoring the old behavior in Swift 3 mode by adding a very explicit
overload.

Fixes: <rdar://problem/32024978>

* [stdlib] Fixing another compatibility issue with [String].flatMap
2017-05-11 04:05:54 -07:00

43 lines
965 B
Swift

// RUN: rm -rf %t ; mkdir -p %t
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
import StdlibUnittest
#if swift(>=4)
public typealias ExpectedResultType = [Character]
#else
public typealias ExpectedResultType = [String]
#endif
var Tests = TestSuite("StringFlatMap")
Tests.test("DefaultReturnType") {
var result = ["hello", "world"].flatMap { $0 }
expectType(ExpectedResultType.self, &result)
}
Tests.test("ExplicitTypeContext") {
expectEqualSequence(["hello", "world"],
["hello", "world"].flatMap { $0 } as [String])
expectEqualSequence("helloworld".characters,
["hello", "world"].flatMap { $0 } as [Character])
}
Tests.test("inference") {
let result = [1, 2].flatMap { x in
if String(x) == "foo" {
return "bar"
} else {
return nil
}
}
expectEqualSequence([], result)
}
runAllTests()