如何在元组上定义后缀运算符?

时间:2019-03-22 17:39:30

标签: swift tuples custom-operator

我有以下代码:

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)'。有任何解决方法的想法吗?

1 个答案:

答案 0 :(得分:3)

这是一个已知的错误,请在Swift论坛中比较Prefix and postfix operators not working for tuple typesSR-294 Strange errors for unary prefix operator with tuple arg

它已针对Swift 5进行了修复,可以在Xcode 10.2 beta 4中编译并运行:

url = "https://api.dev.name.com/v4/domains:checkAvailability"

Set request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
request.open("POST", url, False, "username", "token")
request.setRequestHeader "domainNames", Range("C1").Value & ".com"
request.Send

MsgBox request.ResponseText