If I write the following everything works as expected:
object Working extends App {
val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl
(1 to 10) foreach {n => println(whl(n)) }
}
However, if I wrap my LazyList
in a def
:
object NotWorking extends App {
def f(): Unit = {
val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl
(1 to 10) foreach {n => println(whl(n)) }
}
f()
}
then I get an error message
forward reference extends over definition of value whl val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl
Does anyone know why this happens? I am using Scala 2.13.5 with IntelliJ Idea, if this makes any difference.