Scala has no ++ or -- operator; how to increment or decrement an integer?

Scala FAQ: Scala doesn't have the ++ and -- operators; are the some similar operators or methods that I can use instead?


Because val fields are immutable, they can’t be incremented or decremented, but var integer fields can be mutated with Scala’s += and −= methods:

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

scala> i += 1

scala> println(i)

Next the -= operator: