2

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...

Community
  • 1
  • 1
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149

1 Answers1

7

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 call a += b will be automatically expanded to a = a + b unless a direct method += exists. Thus:

var myInt = 5
myInt += 1
0__
  • 66,707
  • 21
  • 171
  • 266