Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return [+++]}
as Any) return [+++]}
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

[+++]

但这些做到:

[+++] [+++] 总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return }
as Any) return [+++]}
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

[+++]

但这些做到:

[+++] [+++] 总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return }
as Any) return }
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

[+++]

但这些做到:

[+++] [+++] 总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return }
as Any) return }
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

但这些做到:

[+++] [+++] 总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 9, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return }
as Any) return }
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

但这些做到:

[+++] 总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
swift – flatMap API契约如何转换可选输入到非可选结果?_app_内存溢出

swift – flatMap API契约如何转换可选输入到非可选结果?

swift – flatMap API契约如何转换可选输入到非可选结果?,第1张

概述这是Swift 3.0.2中flatMap的契约 public struct Array<Element> : RandomAccessCollection, MutableCollection { public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows 这是Swift 3.0.2中flatMap的契约
public struct Array<Element> : RandomAccessCollection,MutableCollection {    public func flatMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]}

如果我取一个[String?]数组,则flatMap返回[String]

let albums = ["Fearless",nil,"Speak Now","Red"]let result = albums.flatMap { 
// error: Unable to infer complex closure return type; add explicit type to disambiguate.let result = albums.flatMap {    print(
// explicitly annotate [ElementOfResult] to be [String] – thus ElementOfResult == String.let result: [String] = albums.flatMap {    print(
// explicitly annotate ElementOfResult? to be String? – thus ElementOfResult == String.let result = albums.flatMap { element -> String? in    print(element as Any)    return element}
as Any) return }
as Any) return }
}type(of: result) // Array<String>.Type

这里ElementOfResult变成String,为什么不是String? ?泛型类型系统如何从表达式中删除Optional部分?

当您使用身份转换{$0}时,编译器会推断出ElementOfResult? (转换的结果)等效于Element(转换的参数).在这种情况下,Element是String?,因此ElementOfResult? ==字符串?这里不需要可选的促销,因此可以推断ElementOfResult是String.

因此,在这种情况下flatMap(_ :)返回[String].

在内部,这个转换是从闭包返回的ElementOfResult?通过有条件地展开可选项来完成ElementOfResult,如果成功,则将未展开的值附加到结果中.你可以看到exact implementation here.

作为附录,请注意as Martin points out,闭包只有在单语句闭包时才会参与类型推断(见this related bug report).其原因是Jordan Rose in this mailing list discussion:

Swift’s type inference is currently statement-orIEnted,so there’s no easy way to do [multiple-statement closure] inference. This is at least partly a compilation-time concern: Swift’s type system allows many more possible conversions than,say,Haskell or OCaml,so solving the types for an entire multi-statement function is not a trivial problem,possibly not a tractable problem.

这意味着对于具有多个语句的闭包,这些语句传递给map(_ :)或flatMap(_ :)(其中结果类型是通用占位符)等方法,您必须显式地注释闭包的返回类型,或方法返回自己.

例如,这不编译:

但这些做到:

总结

以上是内存溢出为你收集整理的swift – flatMap API契约如何转换可选输入到非可选结果?全部内容,希望文章能够帮你解决swift – flatMap API契约如何转换可选输入到非可选结果?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1049891.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存