为什么我不能在此默认参数值中使用 shorthand 参数名称?
Why can't I use the shorthand argument names in this default parameter value?
以下代码未编译,但在我看来它应该:
public typealias ACallback = (first: [Int], second: String, third: String, fourth: CustomType) -> [Int];
public func doSomething(callback: ACallback = { [=11=] } {
...
}
我在函数声明的行上收到一个错误:
'(first: [Int], second: String, third: String, fourth: CustomType)' is not convertible to '[Int]'
当我这样声明函数时,它起作用了:
public fund doSomething(callback: ACallback = { first, _, _, _ in first } {
如果我用相同的内联定义替换 ACallback
也没有区别。
callback: ACallback = { [=10=].0 }
我觉得应该可以吧。如错误所述,[=11=]
表示此处闭包的整个参数列表的元组。
虽然问题不是重复的,但 this question 的答案似乎解释了这种行为。
要回答您评论中的问题,您可以将类型重新定义为结构或 class(任何合适的),并能够将参数称为属性(而不是位置编号元组)。
以下代码未编译,但在我看来它应该:
public typealias ACallback = (first: [Int], second: String, third: String, fourth: CustomType) -> [Int];
public func doSomething(callback: ACallback = { [=11=] } {
...
}
我在函数声明的行上收到一个错误:
'(first: [Int], second: String, third: String, fourth: CustomType)' is not convertible to '[Int]'
当我这样声明函数时,它起作用了:
public fund doSomething(callback: ACallback = { first, _, _, _ in first } {
如果我用相同的内联定义替换 ACallback
也没有区别。
callback: ACallback = { [=10=].0 }
我觉得应该可以吧。如错误所述,[=11=]
表示此处闭包的整个参数列表的元组。
虽然问题不是重复的,但 this question 的答案似乎解释了这种行为。
要回答您评论中的问题,您可以将类型重新定义为结构或 class(任何合适的),并能够将参数称为属性(而不是位置编号元组)。