Implementation of extensions doesn't match expectation or documentation

I agree. Ideally, we’d have the following situation:

  • Right-associative class methods are disallowed.
  • The only way to define a right-associative method is as an extension method.
  • The order of formal parameters is the same as the order of actual arguments (which corresponds to the current rule for extension methods)

Since Scala did not have extension methods originally, we had to make them normal methods, and that meant we needed this weird swap of the argument. That was a cludge. Let’s not elevate it to a principle by making the same mistake for extension methods where it makes even less sense.

I hope that over time, we will arrive at the ideal state by deprecating right associative member methods. E.g. List would become

class List[+A]:
  def prepend(elem: A): List[A]
  ...
object List:
  extension [A](x: A) def :: (xs: List[A]) = xs.prepend(x)

No weird swapping of arguments; everything is obvious.

12 Likes