如何在元组上定义后缀运算符?
How can I define a postfix operator on a tuple?
我有以下代码:
postfix operator ^^^
public postfix func ^^^(lhs: Int) -> Int {
return 0
}
public postfix func ^^^<T>(lhs: (T, T)) -> [T] {
return [lhs.0, lhs.1]
}
func go() {
1^^^ // this works
(0, 0)^^^ // error: Unary operator '^^^' cannot be applied to an operand of type '(Int, Int)'
}
为此我得到了错误,Unary operator '^^^' cannot be applied to an operand of type '(Int, Int)'
。有什么解决办法吗?
这是一个已知错误,比较 Swift 论坛中的 Prefix and postfix operators not working for tuple types 和
SR-294 Strange errors for unary prefix operator with tuple arg.
已为 Swift 5 修复,以下在 Xcode 10.2 beta 4 中编译和运行:
postfix operator ^^^
public postfix func ^^^<T>(lhs: (T, T)) -> [T] {
return [lhs.0, lhs.1]
}
let x = (0, 0)^^^
print(x) // [0, 0]
我有以下代码:
postfix operator ^^^
public postfix func ^^^(lhs: Int) -> Int {
return 0
}
public postfix func ^^^<T>(lhs: (T, T)) -> [T] {
return [lhs.0, lhs.1]
}
func go() {
1^^^ // this works
(0, 0)^^^ // error: Unary operator '^^^' cannot be applied to an operand of type '(Int, Int)'
}
为此我得到了错误,Unary operator '^^^' cannot be applied to an operand of type '(Int, Int)'
。有什么解决办法吗?
这是一个已知错误,比较 Swift 论坛中的 Prefix and postfix operators not working for tuple types 和 SR-294 Strange errors for unary prefix operator with tuple arg.
已为 Swift 5 修复,以下在 Xcode 10.2 beta 4 中编译和运行:
postfix operator ^^^
public postfix func ^^^<T>(lhs: (T, T)) -> [T] {
return [lhs.0, lhs.1]
}
let x = (0, 0)^^^
print(x) // [0, 0]