title: Create proper late initialization of an abstract val in trait
body: <p>Consider the following case:</p>
  
  <pre><code>trait A {
      protected val mydata = ???
  
      def f(args) = ??? //uses mydata
  }
  
  class B
  class C
  
  class D(arg1: String) extends B with A {
      override val mydata = ??? /// some calculation based on arg1
  }
  
  class E(arg1: String) extends C with A{
      override val mydata = ??? /// some calculation based on arg1
  }
  </code></pre>
  
  <p>A must be a trait as it is used by different unrelated classes. The problem is how to implement the definition of mydata.</p>
  
  <p>The standard way (suggested in many places would be to define mydata as def and override it in the children. However, if f assumes mydata never changes then it can cause issues when some child extends with a function which changes between calls instead of with a val.</p>
  
  <p>Another way would be to do:</p>
  
  <pre><code>trait A {
    protected val mydata = g
    protected def g()
  }
  </code></pre>
  
  <p>The problem with this (beyond adding another function) is that if g depends on construction variables in the child then these must become members of the child (which can be a problem for example if the data is large and given in the construction):</p>
  
  <pre><code> class D(arg1: Seq[String]) {
       def g() = ??? // some operation on arg1
   }
  </code></pre>
  
  <p>If I leave the val in the trait as abstract I can reach issues such as those found <a href="https://stackoverflow.com/questions/7846119/is-there-something-wrong-with-an-abstract-value-used-in-trait-in-scala">here</a>).</p>
  
  <p>What I am looking for is a way to define the value of the val in the children, ensuring it would be a val and without having to save data for late calculations. Something similar as to how in java I can define a final val and fill it in the constructor</p>