title: Recursive call of `val` in scala trait?
body: <p>I am trying to debug a scala program. I found a trait which behavior is similar to following:</p>
  <pre><code>trait A {
      val a: Int = b
      val b: Int = a
  }
  
  class B extends A
  </code></pre>
  <p>If we look at the trait there is cyclic assignment. <code>val</code> <code>b</code> is not even defined but used in definition of <code>val</code> <code>a</code>.</p>
  <p>The above program compile successfully. Both <code>a</code> and <code>b</code> is <code>zero</code>. I tried to the same with variable type string and then value was <code>null</code>.</p>
  <p>There is a third class that override <code>val a</code> similar to following</p>
  <pre><code>class C extends A {
      override val a: Int = 10
  }
  </code></pre>
  <p>Now if an instance of class <code>C</code> is created value <code>a</code> is 10 but <code>val</code> of <code>b</code> is still <code>0</code>.</p>
  <p>Finally, I checked further and instead of overriding a in the body of class, when I override it in constructor parameter similar to following:</p>
  <pre><code>class D(override val a: Int) extends A
  val d = new D(10)
  </code></pre>
  <p>then both <code>d.a</code> and <code>d.b</code> is initialized to 10.</p>
  <p>I wanted to understand that:</p>
  <blockquote>
  <ol>
  <li><p>How definition of <code>val</code> in <code>trait A</code> is valid? In other words how scala interprets the <code>val</code> assignment in <code>trait A</code></p>
  </li>
  <li><p>When a <code>val</code> that is defined in a trait is overridden in the body of a scala class what value is being used in the body of the trait if the same val is used in the trait elsewhere</p>
  </li>
  </ol>
  </blockquote>