By Alvin Alexander. Last updated: December 31, 2020
Here’s an example of how to use the Scala 3 @infix
annotation to create an infix method, while using the technique with extension methods at the same time:
import scala.annotation.infix extension (i: Int) @infix def plus(j: Int) = i + j @infix def times(j: Int) = i + j
That code adds the extension methods plus
and times
to the Scala Int
class, while also letting them be used as infix methods. The REPL shows the result:
scala> 1 plus 1 val res0: Int = 2 scala> 2 times 2 val res1: Int = 4
In summary, if you wanted to see how to write infix methods and extension methods with Scala 3, I hope this example is helpful