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

前端 未结 4 1141
花落未央
花落未央 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:43

    I'd like to add that a Scala Worksheet in the Eclipse-based Scala-IDE (v4.0.0) does not behave like the REPL as one might expect (e.g. https://github.com/scala-ide/scala-worksheet/wiki/Getting-Started says "Worksheets are like a REPL session on steroids") in this respect, but rather like the definition of one long method: That is, forward referencing val definitions (including recursive val definitions) in a worksheet must be made members of some object or class.

提交回复
热议问题