What augmented assignment is
Changing a variable based on its own value is one of the most common things a program does. In Scala you can write it out in full:
var myVar = 10
myVar = myVar + 5
println(myVar)
The right side of = is computed first, using the old value, and the result is stored back into the variable. This prints 15. Because the pattern is so common, Scala offers a shorter form, the augmented assignment:
var myVar = 10
myVar += 5
println(myVar)
It does the same thing and also prints 15. There is one operator for each arithmetic operator:
| Operator | Short for | Example | Result |
|---|---|---|---|
+= | x = x + y | x = 10; x += 5 | 15 |
-= | x = x - y | x = 10; x -= 5 | 5 |
*= | x = x * y | x = 3; x *= 5 | 15 |
/= | x = x / y | x = 20; x /= 4 | 5 |
%= | x = x % y | x = 11; x %= 3 | 2 |
Rules that matter in Scala
The variable must be a var
An augmented assignment changes the variable, so the variable has to be declared with var. A val can't be reassigned. If you write n += 1 where n is a val, the compiler reports:
value += is not a member of Int
The message looks odd, but the fix is simple: declare the variable with var. Scala infers the type Int from a literal such as 18, so you don't need to write it.
Integer division drops the remainder
When both sides are Int, /= performs integer division and throws away the fractional part:
var d = 26
d /= 5
println(d)
This prints 5, not 5.2. Division truncates toward zero, so a variable holding -7 becomes -3 after /= 2. Use %= when you want the remainder instead.
The result stays an Int
The variable keeps its type. With an Int variable, n += 1.5 doesn't compile (the compiler reports the same value += is not a member of Int message), because the sum would be a Double and can't be stored back in an Int. Make the variable a Double or a Long if you need that range of values.
An Int also has a fixed size of 32 bits, and overflow wraps around silently:
var big = Int.MaxValue
big += 1
println(big)
This prints -2147483648. A Long variable has 64 bits and holds much larger values.
There is no ++ or --
Scala has no increment or decrement operators. Writing n++ is a compile error: value ++ is not a member of Int. Use n += 1 and n -= 1 instead.
It works on strings too
+= isn't limited to numbers. On a var holding a String, it appends text:
var s = "ab"
s += "cd"
println(s)
This prints abcd.
An assignment has no useful value
The expression n += 1 changes n but gives back Unit, Scala's "no useful value". Writing val r = { n += 1 } stores () in r, not the new number. To use the new value, read the variable on the next line.
The lesson's exercise
The exercise is a drill that applies one operator after another to a single variable. Follow the value through each step:
- Create
numberwithvarand the value18. number += 8makes it26.number -= 1makes it25.number /= 5makes it5. The division is exact here, so nothing is lost.number += 1makes it6.number *= 3makes it18.number -= 18makes it0.
The complete solution:
var number = 18
number += 8
number -= 1
number /= 5
number += 1
number *= 3
number -= 18
Write down the value after every line, as in the list above. Most mistakes in this drill come from losing track of the current value, especially after the division.
Common mistakes
- Using
val. The first operator that tries to change the variable fails to compile. - Reaching for
++. It doesn't exist in Scala. Use+= 1. - Expecting a decimal from
/=. Two integers divide to an integer. - Mixing up
=+and+=. the operator always starts with the symbol, sox =+ 5is read as one unknown operator=+and doesn't compile.
Key takeaways
x op= yis short forx = x op y, with operators+=,-=,*=,/=and%=.- The variable must be a
var, and it keeps its type. - Integer division truncates, and
Intoverflow wraps silently. - Scala has no
++or--; use+= 1and-= 1.
