[英]Type '()' does not conform to protocol 'IntegerLiteralConvertible'
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
above is a simple example code for Function as first-class type in Swift now, when i call the call the function in the following way: 上面是现在函数在Swift中作为一等类型的简单示例代码,当我以以下方式调用函数时:
var increment = makeIncrementer()
increment(7)
it perfectly gives the answer 它完美地给出了答案
But out of curiosity i tried the direct approach ie 但是出于好奇,我尝试了直接方法,即
makeIncrementer(7) // error
and it gives an error 它给出了一个错误
why is it so??? 为什么会这样??? PS I am a beginner in Swift
PS我是Swift的初学者
The call makeIncrementer()
returns the function, so to call it you pass the parameter in a second set of parentheses: 调用
makeIncrementer()
返回该函数,因此要调用该函数,请在第二组括号中传递参数:
makeIncrementer()(7)
The error message is given because Swift interprets makeIncrementer(7)
as 7
being passed to makeIncrementer
which doesn't take any parameters. 给出错误消息是因为Swift将
makeIncrementer(7)
解释为7
传递给了不带任何参数的makeIncrementer
。 Hopefully Swift error messages are made more friendly in the future. 希望将来Swift错误消息变得更加友好。 While technically correct, the error message given leads to a lot of confusion.
尽管从技术上讲是正确的,但给出的错误消息会引起很多混乱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.