admin管理员组

文章数量:1122832

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:

And the companion object:

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:

And the companion object:

Share Improve this question asked Nov 21, 2024 at 19:46 Cory KleinCory Klein 55.4k47 gold badges203 silver badges259 bronze badges 1
  • 1 I get the downvotes, I do. I myself would probably downvote a question titled "where are the docs for X" too. But FWIW, when I Google "scala postfixops" then the above blank docs for scala.languageFeature.postfixOps were the only results I could get Google to show me. It's easy to miss that there's an extra word Feature in there and you need to go to scala.language.postfixOps instead. – Cory Klein Commented Nov 21, 2024 at 20:38
Add a comment  | 

1 Answer 1

Reset to default 3

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.

本文标签: scaladocWhere is the documentation for scalalanguagepostfixOpsStack Overflow