首页 > 解决方案 > def 中的递归 LazyList 构造给出错误

问题描述

如果我写以下一切都按预期工作:

object Working extends App {
  val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl
  (1 to 10) foreach {n => println(whl(n)) }
}

但是,如果我将我的包装LazyListdef

object NotWorking extends App {
  def f(): Unit = {
    val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl
    (1 to 10) foreach {n => println(whl(n)) }
  }

  f()
}

然后我收到一条错误消息

forward reference extends over definition of value whl val whl: LazyList[Int] = 1 #:: 2 #:: 3 #:: whl

有谁知道为什么会这样?我正在使用带有 IntelliJ Idea 的 Scala 2.13.5,如果这有什么不同的话。

标签: scalalazy-sequences

解决方案


推荐阅读