0

这是上一个问题的后续问题,其中我有一个类型 member 的特征GarageCarType它本身有一个 type member FuelType,我需要一个函数refuel,它可以将 的实例CarType作为第一个参数,并将第一个参数的实例FuelType作为第二个论点。

下面的两个特征的答案是给出Car一个表示类型C <: Car[C]。我现在遇到的问题是我无法弄清楚如何在实现的具体类上定义类型参数Garage,例如ConcreteGarage下面。

trait Fuel

trait Garage {
  type CarType <: Car[CarType]
  def cars: Seq[CarType]

  def copy(cars: Seq[CarType]): Garage

  def refuel(car: CarType, fuel: CarType#FuelType): Garage = copy(
    cars.map {
      case `car` => car.refuel(fuel)
      case other => other
    })
}

trait Car[C <: Car[C]] {
  type FuelType <: Fuel
  def fuel: FuelType

  def copy(fuel: C#FuelType): C

  def refuel(fuel: C#FuelType): C = copy(fuel)
}

class ConcreteGarage(val cars: Seq[ConcreteGarage#CarType]) extends Garage {
  type CarType = Car[CarType] // Nope
  //type CarType = Car[Any] // Nope
  //type CarType = Car[Nothing] // Nope
  //type CarType = Car[Car] // Nope
  //type CarType <: Car[CarType] // Nope
  def copy(cars: Seq[CarType]) = new ConcreteGarage(cars)
}
4

1 回答 1

1

这就是为什么我说要小心在你的余生中必须携带另一个类型参数的丑陋:)

class ConcreteGarage[C <: Car[C]](val cars: Seq[C]) extends Garage {
  type CarType = C
  def copy(cars: Seq[C]) = new ConcreteGarage(cars)
}

当然,如果你有一个特定的车库,它就更简单了。

case class Benzin(litres: Int) extends Fuel
case class Mustang(fuel: Benzin) extends Car[Mustang] {
   type FuelType = Benzin
   def copy(fuel: Benzin) = Mustang(fuel)
}
case class MustangGarage(cars: Seq[Mustang]) extends Garage {
   type CarType = Mustang
   def copy(cars: Seq[Mustang]) = MustangGarage(cars)
}

val m = Mustang(Benzin(0))
val g0 = MustangGarage(Seq(m))
val g1 = g0.refuel(m, Benzin(45))
于 2012-06-30T15:05:47.497 回答