Swift3:现在应该如何使用主体声明操作符优先级组? [英] Swift3 : how to handle precedencegroup now operator should be declare with a body?

查看:105
本文介绍了Swift3:现在应该如何使用主体声明操作符优先级组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作员的前Swift 3代码为:

Former Swift 3 code for operator was:

infix operator × {associativity left precedence 150}

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

But now, as per Xcode 8 beta 6, this generate the following warning:

"operator should not be declared with body"

由于没有文档存在,使用prioritygroup谓词的正确方法是什么?

What's the right way to use precedencegroup predicate as no doc exists right now?

我已经尝试过了,但是不起作用:

I have tried this, but does not work:

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

推荐答案

根据

As per SE-0077, the precedence of an operator is no longer determined by a magic number – instead you now use the higherThan and (if the group resides in another module) lowerThan precedencegroup relationships in order to define precedence relative to other groups.

例如(来自演进建议):

// 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组,该组用于*运算符:

Although in your case, as it appears that your operator is used for multiplication, you could simply use the standard library's MultiplicationPrecedence group, which is used for the * operator:

infix operator × : MultiplicationPrecedence

它定义为:

precedencegroup MultiplicationPrecedence {
  associativity: left
  higherThan: AdditionPrecedence
}

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

For a full list of standard library precedence groups, as well as more info about this change, see the evolution proposal.

这篇关于Swift3:现在应该如何使用主体声明操作符优先级组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆