是否可以在Closures中使用变量参数?

问题描述 投票:-2回答:1

翻新问题

Swift。变量参数与闭包的结合使用。


是否可以用变量参数做闭包?

func this(_ hello: (Int...) -> ()) {}
func that(_ yeah: Int...) {}

下面是可行的。this(that) 下面是不成功的地方this { foo in }, this { _ in }, this { }


我得到了这个错误。Cannot convert value of type '(_) -> ()' to expected argument type '(Int...) -> ()'


我甚至把所有 Int...Void... 我也有同样的结果 附加警告:When calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'? Replace '(Void...)' with '()' 然而,将Void...替换为()...就成功了。


简化问题


let _: (()...) -> () = { _ in }

导致这个错误。Cannot convert value of type '(_) -> ()' to specified type '(()...) -> ()' 有可能解决这个问题吗?

swift parameters functional-programming closures variadic
1个回答
1
投票

你需要在闭包中包含类型。

this { (foo: Int...) in }

类型推理引擎还没有足够的能力来解决这种情况。我怀疑这与 SR-6030.

© www.soinside.com 2019 - 2024. All rights reserved.