Abstracting Common Behavior with Traits in Scala
<p>I have a trait that does the following:</p>

<pre><code>trait MyTrait[T] {
  def doSomething(elems: Seq[T])
}
</code></pre>

<p>I then have a factory that would create instances of the implementations of this trait:</p>

<pre><code>object MyTrait {
  def apply(): MyTrait = {
    new StringTrait()
  }
}
</code></pre>

<p>Now the concrete implementation looks like this:</p>

<pre><code>class StringTrait extends MyTrait[String] {
  def doSomething(elems: Seq[String]) = {
    // some generic logic here
    // some specific logic here (this code bit depends on the type of implementation)
    // some generic logic here 
  }
}
</code></pre>

<p>How could I now make StringTrait such that I pass in specific behavior from it and having the generic logic defined in a abstract class? One way would be to pass in the behavior as a thunk, but that wold mean that I have to modify my doSomething(...) method to take an additional parameter which I would prefer to avoid.</p>