[英]Tuple and if in Scala
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.
如果我取出这个语句,代码编译并运行良好,所以问题特别在这个语句中。
As mentioned here , you can't have tuple assignments to pre-existing references - it's something that Scala does not support.正如这里提到的,您不能对预先存在的引用进行元组分配 - 这是 Scala 不支持的。
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).但是,如果
bestModel
和bestEvals
是预先声明的,那么您将重新声明它们(而不是为它们分配新值)。
Hope this helps!希望这可以帮助!
Cheers.干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.