说明
- 操作符预定义 prefix infix postfix
- prefix: 运算符在运算值的前方; postfix:运算符在运算值的后方;infix:运算符在运算值之间
- precedencegroup:定义运算符的其它属性
prefix operator ^
postfix operator ^-
infix operator ^^
代码示例
prefix operator ^
prefix func ^ (vector: Double) -> Double {
return pow(2, vector)
}
print(^5)
postfix operator ^-
postfix func ^- (vector: Int) -> Int {
return vector * vector
}
print(5^-)
precedencegroup MinePrecedence {
higherThan: AdditionPrecedence
associativity: left
assignment: false
}
infix operator ^^: MinePrecedence
func ^^(left: Int, right: Int) -> Int {
return left + right * 2
}
print(2 ^^ 10 + 2 ^^ 3)