1

I am getting a compile time error with the following code:

val bestModel = model
val bestEvals: List[Double] = null   

... <code, including code that initializes bestEvals> ...

(bestModel, bestEvals) = if (allAgreeBetter)
  (updatedModel, currentEvals.map {case (eval, _) => eval}.toList)
else
  (bestModel, bestEvals)

The error is (are):

Error:(203, 34) ';' expected but '=' found.
      (bestModel, bestEvals) = if (allAgreeBetter)
Error:(205, 11) ';' expected but 'else' found.
      else

What did I miss? If I take out this statement, the code compiles and runs fine, so the problem is specifically in this statement.

Frank
  • 4,341
  • 8
  • 41
  • 57
  • 2
    Adding to the answer, you can't re-assign values to a `val` binding. You'll need to use a `var`. For the multiple assignment, it's best if you do it just one per line, like `if (x) { y = z; a = b } else { y = z2; a = b2 }`. Also, `{ case (eval, _) => eval }` can be expressed more concisely as `_._1`. – Yawar Oct 13 '16 at 02:05

1 Answers1

1

As mentioned here, you can't have tuple assignments to pre-existing references - it's something that Scala does not support.

The only similar pattern that would indeed work would be:

var (bestModel, bestEvals) = if (allAgreeBetter) ...

However, if bestModel and bestEvals were pre-declared, then you would be redeclaring them (and not assigning them a new value).

Hope this helps!

Cheers.

Community
  • 1
  • 1