It seems that a match expression does not have precedence to the right of a right-associative operation (checked it both in Scala 2 and 3):
sealed trait Dir
case object Left extends Dir
case object Right extends Dir
implicit class Ext(arg: Int) {
def := (arg2: Int): Unit = ???
}
object Test {
val dir: Dir = ???
val x: Int = ???
x := dir match { //error
case Left => 2
case Right => 4
}
}
So the compiler does (x := dir) match ... instead of x := (dir match ...).
I’m not an expert in understanding the parser spec. Is this the expected behavior? I found it incredibly surprising.
In any case, even without right-associativity, assignment operators should have the lowest priority nonetheless, no? How can match have a lower priority to an assignment?
Expr1 ::=
...
| [SimpleExpr ‘.’] id ‘=’ Expr
| PostfixExpr
| PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’ // more specific
PostfixExpr ::= InfixExpr ...
InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr // a op b
From this it seems the regular assignment = has a slightly higher priority than the match (both are Expr1, but the assignment comes before), but this is not the case of infix expressions (InfixExpr), even if the operator contains the = symbol (because it simply isn’t checked)