Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular dependencies through fields in ADT result in null values #12700

Closed
helgi98 opened this issue Dec 8, 2022 · 2 comments
Closed

Circular dependencies through fields in ADT result in null values #12700

helgi98 opened this issue Dec 8, 2022 · 2 comments

Comments

@helgi98
Copy link

helgi98 commented Dec 8, 2022

Reproduction steps

Scala version: 2.3.8

sealed abstract class RPS(val score: Int, val beats: RPS)

case object Rock extends RPS(1, Scissors)

case object Paper extends RPS(2, Rock)

case object Scissors extends RPS(3, Paper)

println(Rock.beats) // prints Scissors
println(Paper.beats) // prints null
println(Scissors.beats) // prints Paper

Problem

Paper.beats should be evaluated to Rock and not null.
When you evaluate only Paper.beats it works just fine, but if all three are printed then it produces null.

@som-snytt
Copy link

som-snytt commented Dec 8, 2022

Someone just posted an issue about initialization of circular dependencies. I don't know if this is a "normal" modelling concern or exacerbated by the season of puzzling.

Consider the local case:

object X {
  sealed abstract class RPS(val score: Int, val beats: RPS)

  case object Rock extends RPS(1, Scissors)

  case object Paper extends RPS(2, Rock)

  case object Scissors extends RPS(3, Paper)
}


object Test extends App {
  import X._
  println(Rock.beats) // prints Scissors
  println(Paper.beats) // prints null
  println(Scissors.beats) // prints Paper
  def f: Unit = {
    sealed abstract class RPS(val score: Int, val beats: RPS)
    case object Rock extends RPS(1, Scissors)
    case object Paper extends RPS(2, Rock)
    case object Scissors extends RPS(3, Paper)
    println(Rock.beats) // prints Scissors
    println(Paper.beats) // prints null
    println(Scissors.beats) // prints Paper
  }
  f
}

This issue and links from there: scala/scala3#16456

@som-snytt
Copy link

directly duplicates #9261

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants