如何在scala中将值重新分配给def
我正在编写一个解析器,其中包含以下函数:如何在scala中将值重新分配给def,scala,parsing,function,Scala,Parsing,Function,我正在编写一个解析器,其中包含以下函数: def lastop:(Either[RDD[(Int,Array[Float])], Float], Either[RDD[(Int,Array[Float])], Float]) => RDD[(Int,Array[Float])] = add 其中“add”是执行加法的函数。然后我想在我的程序中使用它,如下所示: terms.foreach(t => t match { case nums ~ op => la
def lastop:(Either[RDD[(Int,Array[Float])], Float], Either[RDD[(Int,Array[Float])], Float]) => RDD[(Int,Array[Float])] = add
其中“add”是执行加法的函数。然后我想在我的程序中使用它,如下所示:
terms.foreach(t =>
t match { case nums ~ op => lastop = op; stack = reduce(stack ++ nums, op)}
我得到以下错误:
[error] /home/mahsa/calculator/temp/ScalaParser.scala:183: reassignment to val
[error] t match { case nums ~ op => lastop = op; stack = reduce(stack ++ nums, op)}
[error] ^
不知道如何解决这个错误 要存储对要调用的函数的更改引用。如果您正在存储和重新分配某些内容,这意味着您需要var,而不是val或def。尝试声明lastop,如:
var lastop:(Either[RDD[(Int,Array[Float])], Float], Either[RDD[(Int,Array[Float])], Float]) => RDD[(Int,Array[Float])] = add
注意,您仍然需要像调用函数一样调用lastop,因为检索var的值将返回一个函数。这是一个微妙但重要的区别