Is there a way I can write this Java code in Scala:
int foo () { return this.i++; }
other than:
def foo : int = {
val t = this.i
this.i += 1
t
}
Is there a way I can write this Java code in Scala:
int foo () { return this.i++; }
other than:
def foo : int = {
val t = this.i
this.i += 1
t
}
No, there's not. Scala doesn't have an equivalent of Java's postfix increment operator.
Your code is the proper way to get the same behaviour (assuming you really need it).
Sure: def foo = copy(i = i+1)
;)
Don't use mutable variables, they are evil. 99% of your real-life code in scala won't need mutable state, so, you should just pretend there is no var
keyword at all in scala until you get familiar with the language concept enough to be able to tell when using a mutable variable is actually a good idea.