Note that there are some explanatory texts on larger screens.

plurals
  1. POScala: Making concrete classes of trait with type member that has a representation type
    primarykey
    data
    text
    <p>This is a follow-up to a <a href="https://stackoverflow.com/questions/11236122/">previous question</a> where I had a trait <code>Garage</code> with a type member <code>CarType</code>, which itself had a type member <code>FuelType</code>, and I needed a function <code>refuel</code> that could take an instance of <code>CarType</code> as the first argument and an instance of the first argument's <code>FuelType</code> as the second argument.</p> <p>The answer, the two traits below, was to give <code>Car</code> a representation type <code>C &lt;: Car[C]</code>. The problem that I now have is that I can't figure out how to define the type parameter on the concrete classes implementing <code>Garage</code>, e.g. <code>ConcreteGarage</code> below.</p> <pre><code>trait Fuel trait Garage { type CarType &lt;: 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` =&gt; car.refuel(fuel) case other =&gt; other }) } trait Car[C &lt;: Car[C]] { type FuelType &lt;: 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 &lt;: Car[CarType] // Nope def copy(cars: Seq[CarType]) = new ConcreteGarage(cars) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload