There are no ++ or -- operators in Scala (use += or -=)

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator:

scala> i--
<console>:9: error: value -- is not a member of Int
              i--
               ^

scala> i -= 1

scala> println(i)
1

The reason there are no ++ or -- operators in the language has to do with "language purity"; allowing for them would have meant adding a special rule to the language, and they wanted to avoid that.

Post new comment

The content of this field is kept private and will not be shown publicly.