title: Scala dependency injection with abstract classes
body: <p>Base class</p>
  
  <pre><code> abstract class BaseSourceReader[T &lt;: BaseSource] {
  
     /**                                                                                                                                                                               
      * Method to read data from source                                                                                                                                               
      */
     def readFromSource(identifier: T): Seq[String]
   }
  
  // Trait common to all source identifiers
  trait BaseSource
  </code></pre>
  
  <p>Derived class</p>
  
  <pre><code> class XYZSourceReader(param1: String) extends BaseSourceReader[XYZBaseSource] {
  
     override def readFromSource(identifier: XYZBaseSource): Seq[String] = 
       // some implementation
   }
  
   case class XYZBaseSource(
       paramA: String,
       paramB: Seq[String]) extends BaseSource
  </code></pre>
  
  <p>Now what I want to do is inject the base source reader to a generic class to make the implementation independent of source:</p>
  
  <pre><code> class MySourceTrasformerJob(
       val sourceReader: BaseSourceReader[BaseSource]) {
       // some code
   }
  </code></pre>
  
  <p>And use it like this:</p>
  
  <pre><code> class MyTransformerJobApp {
      val reader = new XYZSourceReader(param)
      val job = MySourceTrasformerJob(reader)
   }
  </code></pre>
  
  <p>For this code snippet to work, the compiler suggests <code>class SourceReader is invariant in type T. You may wish to define T as +T instead. (SLS 4.5)</code></p>
  
  <p>I tried updating the <code>BaseSourceReader</code>:</p>
  
  <p><code>- abstract class BaseSourceReader[T &lt;: BaseSource] {
  +  abstract class BaseSourceReader[+T &lt;: BaseSource] {</code></p>
  
  <p>but that leads to an error with the <code>readFromSource</code> method error: covariant type T occurs in contravariant position in type T of value identifier.</p>
  
  <p>One working solution that I found is tying the implementation to the source but its not "generic" enough implementation:</p>
  
  <pre><code> class MySourceTrasformerJob(
       val sourceReader: BaseSourceReader[XYZBaseSource]) {
       // some code
   }
  </code></pre>
  
  <p>I am kind of stuck in a loop with this and trying out model updates, any suggested way to handle this but definitely sticking to the generic abstract class dependency injection?</p>