Below is the definition of Stream type taken from fp in scala chapter 5
sealed trait Stream[+A]
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
We can write the following method that will return an infinite stream of a
def constant[A](a: A): Stream[A] = {
lazy val x: Stream[A] = Cons(() => a, () => x)
x
}
What I do not quite understand is, why the compiler doesn't throw forward reference extends over definition of value when x is defined as lazy val (otherwise it does)?
I found an old post here: https://www.scala-lang.org/old/node/6502 but still looking for a clear explanation.