我正在检查.lazy的高阶函数,并且得到了一些与flatMap函数相关的有趣编译错误(可能还有其他函数)
@H_502_1@例子
let array = [1,2,3,4,5,6] array .flatMap { print("DD") return $0 // Cannot convert return expression of type 'Int' to return type 'String?' } .forEach { print("SS") print($0) }@H_502_1@评论一下
array .flatMap { // print("DD") return $0 } .forEach { print("SS") print($0) }@H_502_1@一切正常……更有趣的例子
array .flatMap { let z = $0 return $0 // Or return z - all is the same "Cannot convert return expression of type 'Int' to return type 'String?'" } .forEach { print("SS") print($0) }@H_502_1@什么可能导致这种行为?
目前Sequence上的flatMap(_ :)方法(从Swift 4开始)有两个不同的含义:
@H_502_1@>它可以采用转换闭包返回一个可选的T ?,它将返回一个[T],过滤掉nil结果(在未来版本中将此过载is to be renamed转换为compactMap(_ :)).
public func flatMap<ElementOfResult>( _ transform: (Element) throws -> ElementOfResult? ) rethrows -> [ElementOfResult]@H_502_1@>它可以采用返回Sequence的转换闭包,它将返回一个包含所有结果序列的串联的数组.
public func flatMap<SegmentOfResult : Sequence>( _ transform: (Element) throws -> SegmentOfResult ) rethrows -> [SegmentOfResult.Element]@H_502_1@现在,在Swift 4中,String成为RangeReplaceableCollection(因此也是一个Sequence).所以Swift 3代码做到了这一点:
// returns ["foo"],as using the `nil` filtering flatMap,the elements in the closure // are implicitly promoted to optional strings. ["foo"].flatMap { $0 }@H_502_1@现在这样做:
// returns ["f","o","o"],a [Character],as using the Sequence concatenation flatMap,// as String is now a Sequence (compiler favours this overload as it avoids the implicit // conversion from String to String?) ["foo"].flatMap { $0 }@H_502_1@为了保持源兼容性,专门的flatMap为字符串重载were added:
@H_502_1@06004@H_502_1@这样上面的用法仍会在Swift 3兼容模式下返回[String],而在Swift 4中返回[Character]. @H_502_1@那么,为什么呢
let array = [1,6] array .flatMap { print("DD") return $0 // Cannot convert return expression of type 'Int' to return type 'String?' } .forEach { print("SS") print($0) }@H_502_1@告诉你闭包应该返回一个字符串? @H_502_1@好吧,Swift目前不会推断多语句闭包的参数和返回类型(更多信息请参见this Q&A).所以flatMap(_ :)重载了闭包返回泛型T的位置?或者通用S:序列没有显式类型注释就没有资格被调用,因为它们需要类型推断来满足通用占位符. @H_502_1@因此,唯一符合条件的重载是特殊的String源兼容性,因此编译器期望闭包返回一个String?. @H_502_1@要解决此问题,您可以显式注释闭包的返回类型:
array .flatMap { i -> Int? in print("DD") return i } .forEach { print("SS") print($0) }@H_502_1@但是,如果您实际上没有在实际代码中使用此flatMap(_ :)重载的可选过滤功能,则应使用map(_ :)代替.