Possible Duplicate:
++ operator in Scala
I want to increment an Int variable in scala. But, because Int is immutable, I have to do this
var myInt: Int = 5
....
myInt = myInt + 1
which seems a little too complicated. What I would like to do is
var myInt: Int = 5
....
myInt++
however, since Int is immutable, I can’t do this. Is there any solution? Because I can’t be first who would want to use ++ on integer variable…
A
++operator is not a language construct of Scala, and the desired behaviour cannot be achieved with a regular method definition. But Scala offers at least some syntactic help, in that a calla += bwill be automatically expanded toa = a + bunless a direct method+=exists. Thus: