これは前の質問のフォローアップGarage
であり、型メンバーを持つ特性がありCarType
、それ自体が型メンバーを持っていて、最初の引数としてのインスタンスと最初の引数のインスタンスを取ることができるFuelType
関数が必要でした2番目の引数。refuel
CarType
FuelType
答えは、以下の2つの特性であり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)
}