Scala Tuple Deconstruction

Posted by dbyrne on Stack Overflow See other posts from Stack Overflow or by dbyrne
Published on 2010-05-05T20:46:28Z Indexed on 2010/05/05 20:48 UTC
Read the original article Hit count: 333

Filed under:
|

I am new to Scala, and ran across a small hiccup that has been annoying me.

Initializing two vars in parallel works great: var (x,y) = (1,2)

However I can't find a way to assign new values in parallel: (x,y) = (x+y,y-x) //invalid syntax

I end up writing something like this: val xtmp = x+y; y = x-y; x = xtmp

I realize writing functional code is one way of avoiding this, but there are certain situations where vars just make more sense.

I have two questions:

1) Is there a better way of doing this? Am I missing something?

2) What is the reason for not allowing true parallel assignment?

© Stack Overflow or respective owner

Related posts about scala

Related posts about beginner