gpt4 book ai didi

swift 3 : how to handle precedencegroup now operator should be declare with a body?

转载 作者:IT王子 更新时间:2023-10-29 05:08:57 25 4
gpt4 key购买 nike

运算符的前 Swift 3 代码是:

infix operator × {associativity left precedence 150}

但是现在,根据 Xcode 8 beta 6,这会生成以下警告:

"operator should not be declared with body"

在目前不存在文档的情况下,使用 precedencegroup 谓词的正确方法是什么?

我已经试过了,但是没有用:

infix operator × : times
precedencegroup times {
associativity: left
precedence: 150
}

最佳答案

根据 SE-0077 , 运算符的优先级不再由魔数(Magic Number)决定——相反,您现在使用 higherThan 和(如果该组驻留在另一个模块中)lowerThan precedencegroup 关系以定义相对于其他组的优先级。

例如(来自 evolution proposal ):

// module Swift
precedencegroup Additive { higherThan: Range }
precedencegroup Multiplicative { higherThan: Additive }

// module A
precedencegroup Equivalence {
higherThan: Comparative
lowerThan: Additive // possible, because Additive lies in another module
}
infix operator ~ : Equivalence

1 + 2 ~ 3 // same as (1 + 2) ~ 3, because Additive > Equivalence
1 * 2 ~ 3 // same as (1 * 2) ~ 3, because Multiplicative > Additive > Equivalence
1 < 2 ~ 3 // same as 1 < (2 ~ 3), because Equivalence > Comparative
1 += 2 ~ 3 // same as 1 += (2 ~ 3), because Equivalence > Comparative > Assignment
1 ... 2 ~ 3 // error, because Range and Equivalence are unrelated

尽管在您的情况下,您的运算符似乎用于乘法,但您可以简单地使用标准库的 MultiplicationPrecedence 组,该组用于 * 运算符:

infix operator × : MultiplicationPrecedence

定义为:

precedencegroup MultiplicationPrecedence {
associativity: left
higherThan: AdditionPrecedence
}

有关标准库优先级组的完整列表,以及有关此更改的更多信息,请参阅 evolution proposal .

关于 swift 3 : how to handle precedencegroup now operator should be declare with a body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com