title: Scala How to Define a Method in a Trait to Return a Subclass Generically
body: <p>Given a very simplified model of what I am trying to implement: </p>
  
  <pre><code>sealed trait Data
  case class CustomerData(values: List[String])
  case class EmployeeData(values: List[String])
  </code></pre>
  
  <p>I want to define a method on the trait that requires each of the case classes to define a method that returns a new instance with an additional string. The following doesn't seem to work and it perplexes me but also I want the T to not just me any kind of data but the actual type of the subclass: </p>
  
  <pre><code>sealed trait Data {
    def withNewValue[T &lt;: Data](v : String) : T
  }
  </code></pre>
  
  <p>The subclass then should look like: </p>
  
  <pre><code>case class CustomerData(values: List[String]) {
    override def withNewValue[CustomerData](v : String) : CustomerData = CustomerData(v :: values)
  }
  </code></pre>
  
  <p>But this doesnt compile nor does it restrict the override to require that the type be CustomerData rather than EmployeeData permitting someone to do something silly like: </p>
  
  <pre><code>case class CustomerData(values: List[String]) {
    override def withNew[EmployeeData](v : String) : EmployeeData = EmployeeData(v :: values)
  }
  </code></pre>
  
  <p>Any ideas how I can accomplish what I am going for? </p>