Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

scala> {
     | val test: Stream[Int] = 1 #:: test
     | }
<console>:9: error: forward reference extends over definition of value test
              val test: Stream[Int] = 1 #:: test
                                            ^

scala> val test: Stream[Int] = 1 #:: test
test: 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.

like image 981
senia Avatar asked Mar 31 '12 11:03

senia


3 Answers

Note that in the REPL

scala> val something = "a value"

is evaluated more or less as follows:

object REPL$1 {
  val something = "a value"
}
import REPL$1._

So, any val(or def, etc) is a member of an internal REPL helper object.

Now the point is that classes (and objects) allow forward references on their members:

object ForwardTest {
  def x = y // val x would also compile but with a more confusing result
  val y = 2
}
ForwardTest.x == 2

This is not true for vals inside a block. In a block everything must be defined in linear order. Thus vals are no members anymore but plain variables (or values, resp.). The following does not compile either:

def plainMethod = { // could as well be a simple block
  def x = y
  val y = 2
  x
}

<console>: error: forward reference extends over definition of value y
     def x = y
             ^

It is not recursion which makes the difference. The difference is that classes and objects allow forward references, whereas blocks do not.

like image 97
Debilski Avatar answered Oct 16 '22 19:10

Debilski


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.

like image 40
Daniel C. Sobral Avatar answered Oct 16 '22 20:10

Daniel C. Sobral


The reason for this behavior depends on different val initialization times. If you type val x = 5 directly to the REPL, x becomes a member of an object, which values can be initialized with a default value (null, 0, 0.0, false). In contrast, values in a block can not initialized by default values.

This tends to different behavior:

scala> class X { val x = y+1; val y = 10 }
defined class X

scala> (new X).x
res17: Int = 1

scala> { val x = y+1; val y = 10; x } // compiles only with 2.9.0
res20: Int = 11

In Scala 2.10 the last example does not compile anymore. In 2.9.0 the values are reordered by the compiler to get it to compile. There is a bug report which describes the different initialization times.

like image 32
kiritsuku Avatar answered Oct 16 '22 19:10

kiritsuku