Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

vals in a nested singleton object may be initialized to null #16152

Closed
@matil019

Description

Compiler version

3.0.0, 3.1.3, 3.2.0 and 3.2.2-RC1-bin-20221006-a6a3385-NIGHTLY

Minimized code

final case class Foo(name: String)
object Foo {
 object Foos {
 val Name1 = Foo("Apple")
 val Name2 = Foo("Banana")
 val Name3 = Foo("Cherry")
 // Using explicit `new`s prevents the issue
 // val Name1 = new Foo("Apple")
 // val Name2 = new Foo("Banana")
 // val Name3 = new Foo("Cherry")
 }
 val allFoos: Seq[Foo] = {
 import Foos._
 Seq(Name1, Name2, Name3)
 }
}
object Main {
 def main(args: Array[String]): Unit = {
 println(Foo.Foos.Name1)
 println(Foo.allFoos)
 // Accessing in the reverse order (or not accessing Name1 at all) prevents the issue
 // println(Foo.allFoos)
 // println(Foo.Foos.Name1)
 }
}

Output

Foo(Apple)
List(null, null, null)

Expectation

The output should be:

Foo(Apple)
List(Foo(Apple), Foo(Banana), Foo(Cherry))

Replacing any of the commented part of the program above prevents the issue. Also, Scala 2.13.9 produces the expected output without any modification. So, this is a behavior change between 2.13 and 3.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /