简体   繁体   中英

Intellij Idea not recognizing mutable field in Scala class, gives 'Reassignment to val' error

I have a Scala class with some var fields, let call it A:

class A extends AParent {
    var x: String = _
    ...
}

where AParent has some fields too:

abstract class AParent {
    var id: Long = _
    ...
}

When I try to use class A, to set a value on field x, I get Reassignment to val error. Why that happens when x is var?! Maybe important piece of information is that class A and its parent are located in separate project, being imported via SBT.

Interestingly enough, I do not get the same "error" for any of the fields of AParent class. Only for fields that are directly in class A.

Furthermore, when I run sbt clean compile from command line, everything is ok, no compilation errors. Also, when I go back to Idea and run the application, it runs nicely, does what it should be doing with no errors or warning whatsoever.

This looks like Idea bug to me, to be honest, but I haven't run into similar experiences so far.

Idea version:

IntelliJ IDEA 2018.1.3 (Ultimate Edition)
Build #IU-181.4892.42, built on May 8, 2018
...
JRE: 1.8.0_152-release-1136-b38 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.4

I tried that in Intellij. The only change I needed to do was a default initialization of the var s, like:

class A extends AParent {
  var x: String = _
  def setId(newId: Long) {
    id = newId
  }
}

abstract class AParent {
  var id: Long = _
}

object Appl extends App {
  val a = new A()
  a.setId(234)
  println(a.id)
}

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