title: Scala recursive val behaviour
body: <p>What do you think prints out?</p>
  
  <pre><code>val foo: String = "foo" + foo
  println(foo)
  
  val foo2: Int = 3 + foo2
  println(foo2)
  </code></pre>
  
  <p>Answer:</p>
  
  <pre><code>foonull
  3
  </code></pre>
  
  <p>Why? Is there a part in specification that describes/explains this? </p>
  
  <p>EDIT: To clarify my astonishment - I do realize that <code>foo</code> is undefined at <code>val foo: String = "foo" + foo</code> and that's why it has a default value <code>null</code> (zero for integers). But this doesn't seem very "clean", and I see opinions here that agree with me. I was hoping that compiler would stop me from doing something like that. It does make sense in some particular cases, such as when defining <code>Stream</code>s which are lazy by nature, but for strings and integers I would expect either stopping me due to reassignment to val or telling me that I'm trying to use an undefined value, just like as if I wrote <code>val foo = whatever</code> (given that <code>whatever</code> was never defined).</p>
  
  <p>To further complicate things, @dk14 points out that this behaviour is only present for values represented as fields and doesn't happen within blocks, e.g. </p>
  
  <pre><code>val bar: String = {
    val foo: String = "foo" + foo // error: forward reference extends...
    "bar"
  }
  </code></pre>