2

我有以下代码:

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)'. 任何想法如何解决这一问题?

4

1 回答 1

3

这是一个已知的错误,比较Swift 论坛中的 前缀和后缀运算符不适用于元组类型和SR-294 元组 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]
于 2019-03-22T18:14:36.240 回答