2

Possible Duplicate:
Tuple parameter declaration and assignment oddity

Here's a stupid little function:

def foo() {
  var v1 = 0
  var v2 = 50
  do { 
    var (v1, v2) = someSillyFunction(v1, v2)
  } while(v1 < v2)
}

def someSillyFunction(v1: Int, v2: Int) = (v1+1, v2-1)

Trouble is I'm getting an error "recursive value $x5 needs type" on var (v1, v2). Now, I don't really want to do even what this code is suggesting, as I don't want to -- within the do-block -- declare the new vars v1 and v2. I'd just like to reassign the existing vars. However, trying "(v1, v2) = someSillyFunction(v1, v2)" does me no better. I get '; expected but = found'.

I can always do:

  val (f, g) = someSillyFunction(v1, v2)
  v1 = f
  v2 = g

But that's mighty ugly and I didn't know if there was a better way of reassigning with destructuring.

Community
  • 1
  • 1
Ara Vartanian
  • 1,563
  • 12
  • 18
  • 5
    Duplicate many times: [here](http://stackoverflow.com/questions/1624484/tuple-parameter-declaration-and-assignment-oddity), [here](http://stackoverflow.com/questions/2776651/scala-tuple-deconstruction), and [here](http://stackoverflow.com/questions/6196678/is-it-possible-to-have-tuple-assignment-to-variables-in-scala) – dhg Mar 15 '12 at 05:50

0 Answers0