title: How to use family polymorphism in Scala
body: <p>I have a piece of code that I want to improve, because it have some defaults :</p>
  
  <p>Given the following types (simplified) :</p>
  
  <pre><code>trait Entity
  case object AEntity extends Entity
  case object BEntity extends Entity
  case object CEntity extends Entity
  
  case class A
  case class B
  case class C
  </code></pre>
  
  <p>Here is the code :</p>
  
  <pre><code>  def loaderForEntity : Entity =&gt; EntityLoader[_] = {
      case EntityA =&gt; aLoader
      case EntityB =&gt; bLoader
      case EntityC =&gt; cLoader
    }
  
    type Location = …
    trait EntityLoader[EntityT] extends ((Location, Service) =&gt; EntityT)
    def aLoader = new EntityLoader[A] { def apply(l: Location, store: Service) = store.aForId(l.right.get) }
    def bLoader = new EntityLoader[B] { def apply(l: Location, store: Service) = store.bForId(l.right.get) }
    def cLoader = new EntityLoader[C] { def apply(l: Location, store: Service) = store.cForId(l.right.get) }
  </code></pre>
  
  <p>How to improve this code ?
  How to make aware the type system of the relation between A and EntityA</p>