title: In Scala what is the correct way to fix "error: forward reference extends over definition of value"?
body: <p>In Scala what is the correct way to implement something like this that produces the error "forward reference extends over definition of value b"?</p>
  
  <pre><code>object a {
    def main(args: Array[String]) {
      val b: Map[Int, () =&gt; Int] = Map(5 -&gt; { () =&gt; b.size })
      println(b(5)())
    }
  }
  </code></pre>
  
  <p>Making b lazy works but that doesn't seem like the correct solution.</p>
  
  <p>EDIT: The other question (<a href="https://stackoverflow.com/questions/13328502">What does &quot;Forward reference extends over definition of value&quot; mean in Scala?</a>) is about a bug in Scala itself that where this error is reported incorrectly (or that is what the accepted answer suggests). This question is about what to do when this error is reported correctly. Also I supplied sample code.</p>
  
  <p>Making b lazy seems like overkill because it changes the run-time behavior, when this is a compile-time problem that I assume could be fixed with a different declaration...</p>
  
  <p>As mentioned in the comments, making b a def works too, which seems closer to the solution since def works for recursive functions, but that seems to be reevaluating it at run-time every time it is accessed.</p>
  
  <pre><code>scala&gt; var a = 5
  scala&gt; def b = a
  scala&gt; a = 2
  scala&gt; b
  res0: Int = 2
  scala&gt; a = 1
  scala&gt; b
  res1: Int = 1
  </code></pre>
  
  <p>(with lazy it can be changed before it is accessed but is stored after that)</p>
  
  <p>Both of these seem like run-time solutions to a compile-time problem, I'm looking for something analogous to letrec in Scala.</p>