-3

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
}
JRR
  • 6,014
  • 6
  • 39
  • 59

2 Answers2

0

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
-3

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.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • Shouldn't that at least be `def foo = (i, copy(i = i+1))`? The way you've written it, the behaviour of both returning the old value of `i` and giving `i` a new value isn't kept at all. – sepp2k Jul 03 '17 at 15:46
  • @sepp2k Well, the beauty is you don't need the old value, because the original instance is unchanged, so if you want to know what the value was before it was incremented (and don't like subtracting ones from numbers for some reason ;)), you can just look at the original instance. – Dima Jul 03 '17 at 15:49
  • Fair enough, but this means that your answer doesn't at all reflect the fact that OP wanted the behaviour of postfix increment, rather than prefix increment. OP's method was to be used for both its side-effect and its return value. Your method models the side-effect, but not the return value. So I just don't think your answer has much to do with the question that was asked. – sepp2k Jul 03 '17 at 15:54
  • My answer has very much to do with the question asked: it explains how to _properly_ translate the piece of java code in question into scala. – Dima Jul 03 '17 at 16:00
  • Your answer would be the exact same if the original Java code were `int foo () { return ++this.i; }` or even `void foo() { ++this.i; }`. But if that were the original Java code, the OP wouldn't have had to ask the question in the first place. The point of the question is the behaviour of postfix increment and your question does not address that at all. So as far as I'm concerned, you missed the point of the question. And yes, I get that in many cases avoiding mutation is preferable in Scala, but that wasn't the question nor can you know that this is one of those cases. – sepp2k Jul 03 '17 at 16:05
  • I never said it was "the same" as the original java code. If it was "the same", it would still be java code. This code isn't "the same", but an _equivalent_ code written in proper scala. And yes, I _can_ know this isn't one of those cases. I am sure of it. – Dima Jul 03 '17 at 16:14
  • I've never said anything about "the same" either. All I said is that you didn't address the question's point. – sepp2k Jul 03 '17 at 16:16
  • Yes, and you are wrong about it. The question isn't whether there is a postfix increment operator in scala, it is "how to write this code differently". So, my answer actually addresses the question, while yours doesn't. – Dima Jul 03 '17 at 16:18
  • The question was how to get the behaviour of the postfix increment operator in a way that's different (as in: better, shorter) than what the OP already came up with. Your answer doesn't do that. Mine doesn't either, but that's because it isn't possible, which I pointed out. To be clear: my answer is "you can't", "Scala doesn't have postfix increment" is just part of the explanation of that answer. – sepp2k Jul 03 '17 at 16:20
  • No, it wasn't. Look at it, it literally says: "Is there a way I can write this Java code in Scala: ... _other than_ ...". The question is how to do it in a _different_ way. In fact, it says nothing at all about postfix operators. It's just your imagination. You answer ("you can't") is obviously wrong, because you most definitely _can_ increment numbers in scala. – Dima Jul 03 '17 at 16:26
  • Look at the OP's Scala code. If the OP didn't care about the postfix-increment, he could just do `this.i += 1` and be done with it. But he didn't - he explicitly remembered the old value and returned it. So he obviously cared about the postfix semantics. So yes, I inferred from that this was the point of the question. In my mind that's the only reasonable way to interpret the question, but you obviously disagree. And of course you can increment numbers in Scala (the OP is doing just that after all). What you can't do is get post-increment behaviour better than what the OP did. – sepp2k Jul 03 '17 at 16:29
  • The ops scala code is irrelevant, exactly because his question was how to do it _differently_. The question is not about post-increment in scala. The question is about how to do in scala something, that's done in java with a post-increment. Obviously, he couldn't just do `i+=1` "and be done", because that loses the original value. My answers shows how to avoid losing it. – Dima Jul 03 '17 at 16:35
  • Okay, but even if we interpret the question that way, what's done in Java using postfix increment is "set a new value for i while at the same time using i's old value". Your Scala code doesn't do that (thus my first comment). – sepp2k Jul 03 '17 at 16:38
  • Not "set a new value for i", but "increment i without losing the original value". – Dima Jul 03 '17 at 16:38