简体   繁体   English

为什么代码产生错误类型'(int,int)'不符合协议'IntegerLiteralConvertible'

[英]Why does the code produces error Type '(int, int)' does not conform to protocol 'IntegerLiteralConvertible'

With the code below, I get "Type '(int, int)' does not conform to protocol 'IntegerLiteralConvertible' instead of missing argument as one would expect. What's IntegerLiteralConvertible and why do you think the compiler produces this error instead for the code below? 使用下面的代码,我得到“Type”(int,int)'不符合协议'IntegerLiteralConvertible',而不是像人们期望的那样缺少参数。什么是IntegerLiteralConvertible,为什么你认为编译器会为下面的代码产生这个错误?

I have looked at other SO posts regarding this error but have not gotten any insight from them. 我已经查看了有关此错误的其他SO帖子,但没有从他们那里得到任何见解。

func add(x:Int, y:Int) {

}

add(3)

My best guess is that it tries to convert the (3) tuple into a (Int, Int) tuple. 我最好的猜测是它试图将(3)元组转换为(Int, Int)元组。

In fact, this is accepted by the compiler and works as expected: 实际上,编译器已接受此操作并按预期工作:

func add(x: Int, y: Int) -> Int {
  return x + y
}

let tuple = (4, 7)
add(tuple)

In playground that outputs 11 , which is the expected sum result. 在输出11操场上,这是预期的总和结果。

Note : the code above works if the func is global, with no named parameters. 注意 :如果func是全局的,没有命名参数,则上面的代码可以工作。 If it's an instance or class/static method, then the tuple must include parameter names: 如果它是实例或类/静态方法,那么元组必须包含参数名称:

class MyClass {
    class func add(# x: Int, y: Int) -> Int {
        return x + y
    }
}

let tuple = (x: 3, y: 7)

MyClass.add(tuple) // returns 10

As for IntegerLiteralConvertible , it's used to make a class or struct adopting it to be initializable from a literal integer. 至于IntegerLiteralConvertible ,它用于使一个类或结构采用它从文字整数初始化。 Let's say you have a struct and you want to be able to instantiate by assigning an literal integer, you achieve it this way: 假设您有一个结构,并且希望能够通过分配一个文字整数来实例化,您可以这样实现:

struct MyDataType : IntegerLiteralConvertible {
    var value: Int

    static func convertFromIntegerLiteral(value: IntegerLiteralType) -> MyDataType {
        return MyDataType(value: value)
    }

    init(value: Int) {
        self.value = value
    }
}

and then you can create an instance like this: 然后你可以创建一个这样的实例:

let x: MyDataType = 5

It looks like you are trying to use currying — Swift has built-in support for this, but it is not automatic, so you have to be explicit about it when declaring your function: 看起来你正在尝试使用currying - Swift内置了对此的支持,但它不是自动的,所以在声明你的函数时你必须明确它:

func add(x:Int)(y:Int) -> Int {
    return x + y
}

println(add(3)) // (Function)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;()&#39;不符合协议&#39;IntegerLiteralConvertible&#39; - Type '()' does not conform to protocol 'IntegerLiteralConvertible' 快速错误类型“ T”不符合协议“ IntegerLiteralConvertible” - Swift error Type 'T' does not conform to protocol 'IntegerLiteralConvertible' 类型&#39;String.Index&#39;不符合协议&#39;IntegerLiteralConvertible&#39; - Type 'String.Index' does not conform protocol 'IntegerLiteralConvertible' 类型'Int'不符合协议'BooleanType' - Type 'Int' does not conform to protocol 'BooleanType' 类型 Int 不符合协议序列 - Type Int does not conform to protocol sequence Swift-类型&#39;int&#39;不符合协议&#39;intervaltype&#39; - Swift - Type 'int' does not conform to protocol 'intervaltype' 类型“ Int”不符合协议“ NSCopying” - Type 'Int' does not conform to protocol 'NSCopying' 类型“Int”不符合协议“BooleanType”? - Type 'Int' does not conform to protocol 'BooleanType'? 类型&#39;Int32&#39;不符合协议&#39;AnyObject&#39;Swift? - Type 'Int32' does not conform to protocol 'AnyObject' Swift? 游乐场执行失败: <EXPR> :15:33:错误:类型&#39;Int&#39;不符合协议&#39;BooleanType&#39; - Playground execution failed: <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM