如何在Scala中使用家族多态性

时间:2015-03-14 00:15:53

标签: scala polymorphism

我有一段我想改进的代码,因为它有一些默认值:

鉴于以下类型(简化):

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

以下是代码:

  def loaderForEntity : Entity => EntityLoader[_] = {
    case EntityA => aLoader
    case EntityB => bLoader
    case EntityC => cLoader
  }

  type Location = …
  trait EntityLoader[EntityT] extends ((Location, Service) => 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) }

如何改进此代码? 如何了解A和EntityA之间关系的类型系统

0 个答案:

没有答案