# Right-associative infix operations and match expression precedence surprise

**URL:** <https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021>\
**Category:** Language Design\
**Created:** [February 1, 2025, 11:37pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021 "2025-02-01T23:37:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 1, 2025, 11:37pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/1 "2025-02-01T23:37:58Z")

</div>

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):

```auto
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.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 1, 2025, 11:44pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/2 "2025-02-01T23:44:03Z")

</div>

Isn’t `:=` left-associative ?  
And `=:` would be right-associative

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 1, 2025, 11:48pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/3 "2025-02-01T23:48:35Z")

</div>

Yes, I made several tests. Pasted the wrong example. For right associative it’s a different error, but still an error.

> **[Scastie - An interactive playground for Scala.](https://scastie.scala-lang.org/cEkzmiQVTKyi13hMymnXFQ)**
>
> 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 {
> case Left =\> 2
> case...

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 1, 2025, 11:51pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/4 "2025-02-01T23:51:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 1, 2025, 11:59pm UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/5 "2025-02-01T23:59:08Z")

</div>

[https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html](https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html)

```ebnf
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)

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [February 22, 2025, 12:37am UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/6 "2025-02-22T00:37:04Z")

</div>

This is the motivation for dotted match

```Scala
scala> 1 + 2.match { case i => i * 2 }
val res0: Int = 5

```

though match is not an operator or member.

In Scala 2 the left operand is postfix:

```auto
scala> "hello" length match { case i => i * 2 }
val res0: Int = 10

```

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 22, 2025, 4:12am UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/7 "2025-02-22T04:12:00Z")

</div>

Yeah, I figured out there could be working code that cannot allow changing the precedence.

However, I am considering creating a SIP to allow an `if` after an infix operator. It cirrently fails to compile and ai see no reason why not to allow it.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [February 22, 2025, 8:33am UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/8 "2025-02-22T08:33:56Z")

</div>

There is a suspended PR for indented infix operand.

> <https://github.com/scala/scala3/pull/19099>
>
> Allow indented regions after operators that appear on their own line. With this …change, the following compiles:
> 
> \`\`\`scala
> import language.experimental.fewerBraces
> 
> def test(y: Int) =
> val firstValue = y
> \* y
> val secondValue =
> firstValue
> +
> if firstValue \< 0 then 1 else 0
> +
> if y \< 0 then y else -y
> 
> val result =
> firstValue \< secondValue
> ||
> val thirdValue = firstValue \* secondValue
> thirdValue \> 100
> ||
> def avg(x: Double, y: Double) = (x + y)/2
> avg(firstValue, secondValue) \> 0.0
> ||
> firstValue
> \* secondValue
> \*
> val firstSquare = firstValue \* firstValue
> firstSquare + firstSquare
> \<=
> firstValue \`max\` secondValue
> 
> \`\`\`
> This PR eliminates one of two areas where braces were previously (and annoyingly) still required. It will need a SIP before it is merged.
> 
> I hope that the example shows that requiring braces here is unnecessary and that the code is clearer without them.

My first reaction was that it is quite baroque (and I love baroque), but since I’ve learned a bit more about indentation (and used it more), I think it will be a feature that in retrospect we could never do without.

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 22, 2025, 10:40am UTC](https://contributors.scala-lang.org/t/right-associative-infix-operations-and-match-expression-precedence-surprise/7021/9 "2025-02-22T10:40:07Z")

</div>

It’s not the same SIP. What you referenced still required an indentation before the `if` and did other stuff. I just want to wite something like `val x = 1 + if cond then 2 else 3`.
