let p1 = (name: "John", age:12)let p2 = (color: "Red", size:12)if p1 == p2 { print("equal")}else { print("not equal")} 由于不同的参数名称和不编译的代码,我期望这两个元组不兼容.但它运作正常.很想知道如何.
let p1 = (name: "John", age:12) let p2 = (color: "Red", size:12) if p1 == p2 { print("equal") }else { print("not equal") }
由于不同的参数名称和不编译的代码,我期望这两个元组不兼容.但它运作正常.很想知道如何.
Swift是否根据属性类型自动创建==运算符,然后只做一个简单的lhs与rhs.是吗?
编辑:
func givePerson() -> (name: String, age: Int)? { return ("alex", 2) } func extract() { var p3 : (Name: String, age: Int) if let person = giveName() as? (Name: String, age: Int) { p3 = person print(p3) }else { print("p3 not defined") } } extract() // p3 not defined
我的提取函数失败了,只是因为我得到了arity的名称不正确.这也是预期的吗?这与元组比较有何不同?
标准库 defines tuple comparison operators包含2个元素元组,最多包含6个元素元组.它通过使用元编程工具 GYB(Generate Your Boilerplate)来实现这一点,该工具允许嵌入式Python代码从模板生成文件.生成的元组比较运算符最终看起来像:
public func == <A, B>(lhs: (A, B), rhs: (A, B)) -> Bool public func == <A, B, C>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool public func == <A, B, C, D>(lhs: (A, B, C, D), rhs: (A, B, C, D)) -> Bool // ...
所以用你的代码:
let p1 = (name: "John", age: 12) let p2 = (color: "Red", size: 12) if p1 == p2 { print("equal") } else { print("not equal") }
编译器将调用两元素元组比较运算符.您会注意到,定义的元组比较运算符都没有为其参数使用元组标签.之所以(name:String,age:Int)和(color:String,size:Int)都可以传递给(A,B)的原因是编译器实现了一个可以去除其标签元组的隐式转换.因此,两个参数都剥离了它们的标签,并且都作为(String,Int)传递.
这就是使以下合法的原因:
let p1 = (name: "John", age: 12) let p2: (String, Int) = p1 // Legal.
事情变得更加奇怪,编译器还有一个隐式转换,可以向元组添加任意标签:
let p1 = ("John", 12) let p2: (foo: String, bar: Int) = p1 // Legal.
但是禁止直接重命名标签:
// Indirect renaming ✅ let p1 = (name: "John", age: 12) let p2: (String, Int) = p1 let p3: (foo: String, bar: Int) = p2 // Put together ✅ let p4: (foo: String, bar: Int) = p1 as (String, Int) // Direct renaming ❌ let p4 = (name: "John", age: 12) let p5: (foo: String, bar: Int) = p4
这就是你所看到的行为?投.与上面的示例一样,首先强制转换为未标记的表单:
func givePerson() -> (name: String, age: Int)? { return ("alex", 2) } func extract() { var p3: (Name: String, age: Int) if let person = givePerson() as (String, Int)? as? (Name: String, age: Int) { p3 = person print(p3) } else { print("p3 not defined") } } extract() // (Name: "alex", age: 2)