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:

How to read and write from a file in a Linux bash shell script

I’m currently writing a complicated Linux bash shell script where I need to keep a counter in an external file, and to do so, I need to be able to write to a file and then read from that file.

In short, this is how I write my counter to that file:

# create a variable to represent the filename

# write to the file
echo "0" > $COUNTER_FILE

Later in the code I increment the counter and write it to the file like this:

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: