简体   繁体   中英

How to solve reassignment to val error?

I am writing a parser trying to calculate the result of an expression containing float and an RDD, I have override + - / * and it works fine. In one part I am getting the famous error the "reassignment to val" but cannot figure out how to solve it. Part of the code is as follow:

 def calc: Parser[Any]=rep(term2 ~ operator) ^^ {
  //match a list of term~operator
  case termss =>
    var stack =List[Either[RDD[(Int,Array[Float])], Float]]()
    var lastop:(Either[RDD[(Int,Array[Float])], Float], Either[RDD[(Int,Array[Float])], Float]) => RDD[(Int,Array[Float])] = add
    termss.foreach(t =>
    t match { case nums ~ op => {
                            if (nums=="/path1/test3D.xml")
                                    nums=sv.getInlineArrayRDD()
                            lastop = op; stack = reduce(stack ++ nums, op)}}
    )

   stack.reduceRight((x, y) => lastop(y, x))
}
def term2: Parser[List[Any]] = rep(factor2)
def factor2: Parser[Any] = pathIdent | num | "(" ~> calc <~ ")"
def num: Parser[Float] = floatingPointNumber ^^ (_.toFloat)

I defined pathIdent to parse paths. Here is the error:

    [error]  reassignment to val:
    [error]                                         nums=sv.getInlineArrayRDD()
    [error]                                             ^

I have changed def in term2, factor2, and num to var although I knew it seems incorrect but that's the only thing came into my mind to test and it didn't work. Where is it coming from?

In this piece of code:

case nums ~ op => {
  if (nums=="/path1/test3D.xml")
    nums=sv.getInlineArrayRDD()

The nums isn't reassignable because it comes from the pattern matching (see the case line). The last line ( nums = ... ) is trying to assign to nums when it can't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM