Why can't i define recursive variable in code block?

Posted by senia on Stack Overflow See other posts from Stack Overflow or by senia
Published on 2012-03-31T11:25:57Z Indexed on 2012/03/31 11:28 UTC
Read the original article Hit count: 130

Filed under:
|

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

scala> {
     | val fibs: Stream[Int] = 1 #:: fibs.scanLeft(1){_ + _}
     | }
<console>:9: error: forward reference extends over definition of value fibs
              val fibs: Stream[Int] = 1 #:: fibs.scanLeft(1){_ + _}
                                            ^

scala> val fibs: Stream[Int] = 1 #:: fibs.scanLeft(1){_ + _}
fibs: Stream[Int] = Stream(1, ?)

lazy keyword solves this problem, but i can't understand why it works without a code block but throws a compilation error in a code block.

© Stack Overflow or respective owner

Related posts about scala

Related posts about recursion