简体   繁体   中英

immutable.List source code has “Reassignment to val” error

In Scala version 2.11.8, the List class has reassignment to val errors when I view its source code in Intellij.

For example at line 278 within List.map function source code:

t.tl = nx

IntelliJ shows analyze error for that line stating that there is a reassignment to val. When I checked the source code, 't' is a variable but 'tl' is a constructor parameter which is declared as Val. And here is code snippet that IntelliJ directs me to the declaration of tl:

package scala.collection.immutable
@scala.SerialVersionUID(value = 509929039250432923)
final case class ::[B](override val head : B, private[scala] val tl : scala.collection.immutable.List[B]) extends scala.collection.immutable.List[B] with scala.Product with scala.Serializable {
  override def tail : scala.collection.immutable.List[B] = { /* compiled code */ }
  override def isEmpty : scala.Boolean = { /* compiled code */ }
}

I think that part of the code like the following example:

class Test(val x: String)

var t: Test = new Test("test")
t.x = "test2"

Similarly, IntelliJ shows the same error on assignment tx = "test2". So, what makes the source code works even if there is a reassignment to val error.

IntelliJ version is: IntelliJ IDEA 2016.2.3 Build #IU-162.1812.17, built on August 30, 2016 JRE: 1.8.0_40-b26 amd64 JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation

Can you show the code snippet whereby you claim that tl is a val? From List source, tl is a var not a val

final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
  override def tail : List[B] = tl
  override def isEmpty: Boolean = false
}

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