数组 – 在flatMap中“无法转换返回表达式”,其中包含无意义的表
我正在检查.lazy的高阶函数,并且得到了一些与flatMap函数相关的有趣编译错误(可能还有其他函数)
例子 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) } 评论一下 array .flatMap { // print("DD") return $0 } .forEach { print("SS") print($0) } 一切正常……更有趣的例子 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) } 什么可能导致这种行为?
目前Sequence上的flatMap(_ :)方法(从Swift 4开始)有两个不同的含义:
>它可以采用转换闭包返回一个可选的T ?,它将返回一个[T],过滤掉nil结果(在未来版本中将此过载is to be renamed转换为compactMap(_ :)). public func flatMap<ElementOfResult>( _ transform: (Element) throws -> ElementOfResult? ) rethrows -> [ElementOfResult] >它可以采用返回Sequence的转换闭包,它将返回一个包含所有结果序列的串联的数组. public func flatMap<SegmentOfResult : Sequence>( _ transform: (Element) throws -> SegmentOfResult ) rethrows -> [SegmentOfResult.Element] 现在,在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 } 现在这样做: // 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 } 为了保持源兼容性,专门的flatMap为字符串重载were added:
这样上面的用法仍会在Swift 3兼容模式下返回[String],而在Swift 4中返回[Character]. 那么,为什么呢 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) } 告诉你闭包应该返回一个字符串? 好吧,Swift目前不会推断多语句闭包的参数和返回类型(更多信息请参见this Q&A).所以flatMap(_ :)重载了闭包返回泛型T的位置?或者通用S:序列没有显式类型注释就没有资格被调用,因为它们需要类型推断来满足通用占位符. 因此,唯一符合条件的重载是特殊的String源兼容性,因此编译器期望闭包返回一个String?. 要解决此问题,您可以显式注释闭包的返回类型: array .flatMap { i -> Int? in print("DD") return i } .forEach { print("SS") print($0) } 但是,如果您实际上没有在实际代码中使用此flatMap(_ :)重载的可选过滤功能,则应使用map(_ :)代替. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ruby-on-rails – 在此上下文中不允许部署Capistrano映射时
- OC Swift 混编
- c# – 如何检查dynamic是否为空.
- 使用bazel为具有模拟依赖关系的C构建单元测试
- 如何在Ruby中拆分参数字符串Bash样式?
- dojo之dojox/data/CsvStore初始化
- Flex通过IFrame嵌入html,并调用html中的js脚本
- Tiny210(S5PV210) U-BOOT(五)----Nand Flash源码分析
- ruby-on-rails – 用户授权后Facebook iframe应用程序被重定
- Oracle 补丁那些事儿(PS、PSU、CPU、SPU、BP、DBBP…)