-3

Reading a lesson book, I was testing this simple scala 2.12 code :

var b:ArrayBuffer[Int] = new ArrayBuffer();
b.append(5);
b += (8, 3, 2);

for(i <- 0 to 3 reverse)
  println(b(i));

But I've received the error message :

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 should be explicitly enabled.

Of course, I did the wanted import then, and it worked :

import scala.language.postfixOps

2
3
8
5

But what is that notion of language keywords unlocked by an import?

Implicitly, I've checked that the for keyword is immediately available in Scala,
and I assume that while and most others are too,

but if reverse and some others aren't, it means that there is a list of...
disabled keywords (???)
But I don't know that list, and the reason why they are some that must be explicitly activated.

  • Are they of discouraged use ?
  • Are they deprecated ?
  • Or in an "early preview" state, intended to be really implemented in Scala 2.13 or later, and considered unstable ?
Marc Le Bihan
  • 2,308
  • 2
  • 23
  • 41
  • 3
    Did you do what the error message suggests and read the documentation? What did you find? Can you explain what, *precisely*, you did and didn't understand about the documentation, what you did to research the parts you didn't understand, what you found during that research, and why it didn't help you? That way, you a) prevent answerers from wasting both their time and yours repeating stuff you already know, b) prevent answerers from wasting both their time and yours repeating stuff you already researched and didn't understand, and c) help the Scala developers improve the documentation so that – Jörg W Mittag Jun 11 '21 at 05:44
  • … future developers don't stumble over the same hurdles you did. You would essentially make the world a better place, and who wouldn't want that? Also, can you clarify your question? You mention keywords multiple times both in the title and in the body, but I see nothing in your problem or your solution that relates to keywords. – Jörg W Mittag Jun 11 '21 at 05:45
  • 1
    @JörgWMittag Hey, please calm down. Don't take things so personally. _Scala_ isn't a so ringfenced language that no questions are allowed about it in SO to ease its understanding. Furthermore, it eventually happens that the explanation given by Michal, below, is that a part of its content is deprecated. – Marc Le Bihan Jun 11 '21 at 06:43
  • If you agree with Michal's answer then you should accept it. – Tim Jun 11 '21 at 10:25

1 Answers1

5

This has nothing to do with keywords. import scala.language.postfixOps unlocks a language feature called "postfix notation" or "postfix operators". This lets you call a parameterless method with a space instead of a dot (.) between the "invocation target" and the method name. So instead of writing:

(0 to 3).reverse // "reverse" is just a method

you can do the following:

import scala.language.postfixOps
0 to 3 reverse

Like you suggested, the feature was deprecated and its usage discouraged, because it led to ambiguities. See the documentation and this answer for more details.