scalascaladoc

Where is the documentation for scala.language.postfixOps?


If you try to use a postfix operator, you get an error with an explanation:

scala> Seq(1, 2, 3) reverse
                    ^
       error: postfix operator reverse needs to be enabled
       by making the implicit value scala.language.postfixOps visible.
       This can be achieved by adding the import clause 'import scala.language.postfixOps'
       or by setting the compiler option -language:postfixOps.
       See the Scaladoc for value scala.language.postfixOps for a discussion
       why the feature needs to be explicitly enabled.

In particular, it says:

See the Scaladoc for value scala.language.postfixOps for a discussion

Where is this discussion? When I look at the 2.13 scaladocs entry for postfixOps and its companion objects, they have no discussion or explanation for the existence of this trait/object:

postfixOps trait scaladoc showing no discussion or explanation

And the companion object: postfixOps object scaladoc showing no discussion or explanation


Solution

  • The right place to look at is scala.language.postfixOps.

    Only where this feature is enabled, is postfix operator notation (expr op) permitted. If postfixOps is not enabled, an expression using postfix notation is rejected by the compiler.

    Why keep the feature? Postfix notation is preserved for backward compatibility only. Historically, several DSLs written in Scala need the notation.

    Why control it? Postfix operators interact poorly with semicolon inference. Most programmers avoid them for this reason alone. Postfix syntax is associated with an abuse of infix notation, a op1 b op2 c op3, that can be harder to read than ordinary method invocation with judicious use of parentheses. It is recommended not to enable this feature except for legacy code.