任何人都知道为什么编译器会在方法调用中应用这种优先级?
object Wrap {
case class Vertex(label: String) {
def <--(label: String) = SemiEdge(this, label)
}
case class Edge(from: Vertex, to: Vertex, label: String)
case class SemiEdge(from: Vertex, label: String) {
def -->(to: Vertex) = Edge(from, to, label)
}
// This doesn’t work because the right function is applied first
Vertex("paris") <-- "eurostar" --> Vertex("london")
// This does work
(Vertex("paris") <-- "eurostar") --> Vertex("london")
}
我知道这与operator precedence有关但看起来这是一个错误,在这种情况下没有?
干杯
答案 0 :(得分:2)
运算符的第一个字符确定优先级。因此, - 将在&lt; 之前发生 来自Scala Reference section 6.12.3:
6.12.3中缀操作 中缀运算符可以是任意标识符。中缀运营商具有优先权 关联性定义如下: 中缀运算符的优先级由运算符的第一个字符决定。下面以增加优先顺序列出了字符,同一行中的字符具有相同的优先级。
(所有字母)
|
^
&安培;
&LT; &GT;
=!
:
+ -
* /%
(所有其他特殊字符)
答案 1 :(得分:0)
Scala通过运算符的第一个符号确定运算符优先级。 <--
的第一个符号是<
,-->
的第一个符号是-
。 <
的优先级低于-
,因此<--
的优先级也低于-->
。