Why can't i define a variable recursively in a code block?

前端 未结 4 1148
花落未央
花落未央 2020-12-19 02:35

Why can\'t i define a variable recursively in a code block?

scala> {
     | val test: Stream[Int] = 1 #:: test
     | }
:9: error: forward          


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 02:45

    I'll add that when you write:

    object O {
      val x = y
      val y = 0
    }
    

    You are actually writing this:

    object O {
      val x = this.y
      val y = 0
    }
    

    That little this is what is missing when you declare this stuff inside a definition.

提交回复
热议问题