How to create a Scala 3 infix method (and extension method)

As a brief note, this example shows how to use the Scala 3 infix annotation — @infix. This solution also shows how to use the infix annotation with Scala 3 extension methods, because that’s what I need for my current situation:

import scala.annotation.infix

extension (i: Int)
   @infix def plus(j: Int) = i + j
   @infix def times(j: Int) = i + j

Given those infix + extension method definitions, this is how you use those infix methods:

1 plus 1    // 2
2 times 2   // 4

The REPL confirms this:

scala> 1 plus 1
val res0: Int = 2

scala> 2 times 2
val res1: Int = 4

Before I go, here’s a note from the Dotty / Scala 3 documentation related to infix methods:

A method annotation that suggests that the annotated method should be used as an infix operator. Infix operations with alphanumeric operator names require the operator to be annotated with @infix

In summary, if you wanted to see how to create a Scala 3 infix method, particularly as an extension method, I hope this example is helpful.