我有一段我想改进的代码,因为它有一些默认值:
鉴于以下类型(简化):
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之间关系的类型系统