scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

object member variable initialization #9360

Closed scabug closed 1 year ago

scabug commented 9 years ago
class BaseClass(s: String) {
  def print: Unit = {
    println("hashCode=" + this.hashCode + " | " + this.getClass.getSimpleName + " " + s)
    println(this.getClass.getDeclaringClass.getName)
  }
}

object Object {
  val s: String = "hello"

  object AObj extends BaseClass(s)

  object BObj extends BaseClass(s)

  val list = List(AObj, BObj)

  def print() {
    println(list)
  }
}

object ObjectInit {
  def main(args: Array[String]) {
    Object.AObj.print
    Object.BObj.print
    Object.print
  }
}

Output: AObj in list is null. List(null, fun.Object$BObj$@28d93b30) Expected: initialization of list should be initialized correctly independent from Object.AObj.print/Object.AObj.print.

Note: If No Object.AObj.print/Object.AObj.print be called before Object.print, the initialization is correct.

scabug commented 9 years ago

Imported From: https://issues.scala-lang.org/browse/SI-9360?orig=1 Reporter: JIE HUANG (JIEHUANG) Affected Versions: 2.11.6

som-snytt commented 1 year ago

duplicates https://github.com/scala/bug/issues/9115

where the nested object depends on a member of the enclosing object.