Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在scala中将值重新分配给def_Scala_Parsing_Function - Fatal编程技术网

如何在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的值将返回一个函数。这是一个微妙但重要的区别