Skip to content

Unexpected empty value when accessing object val #15557

Open
@Sporarum

Description

@Sporarum

Compiler version

3.1.3
Rest of scastie settings:

scalacOptions ++= Seq(
  "-deprecation",
  "-encoding", "UTF-8",
  "-feature",
  "-unchecked",
  "-Yexplicit-nulls" // not necessary, but worrying that it works even with it
)

scalaVersion := "3.1.3"
libraryDependencies += "org.scastie" %% "runtime-scala" % "1.0.0-SNAPSHOT"

addSbtPlugin("org.scastie" % "sbt-scastie" % "1.0.0-SNAPSHOT")

Minimized code

object A:
  val a = B.seed

object B:
  val seed = (0,0) // 'c' // 1 // "test"
  val b = A.a

A.a // remove and it works as expected
B.b

https://scastie.scala-lang.org/Sporarum/KrfWCxvTQfqrb7dMoi9NqA/69

Output

A.a => "(0,0): scala.Tuple2[scala.Int, scala.Int]" // "c: scala.Char" // "1: scala.Int" // "test: scala.String"
B.b => "null: scala.Tuple2[scala.Int, scala.Int]"  // ": scala.Char"  // "0: scala.Int" // "null: scala.String"

It is also possible to append B.b._1 to get a NullPointerException in the case where seed is a tuple
Note that this is the case even with -Yexplicit-nulls !

If the call to A.a is removed, or if seed is an inline val (or def in the case of (0,0)):

B.b => "(0,0): scala.Tuple2[scala.Int, scala.Int]" // "c: scala.Char" // "1: scala.Int" // "test: scala.String"

Expectation

The inclusion/removal of the A.a call should not affect the value of B.b !
If those kinds of references are forbidden, an error should be issued

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