Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8200189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:21:16+00:00 2026-06-07T06:21:16+00:00

This is a follow-up to a previous question where I had a trait Garage

  • 0

This is a follow-up to a previous question where I had a trait Garage with a type member CarType, which itself had a type member FuelType, and I needed a function refuel that could take an instance of CarType as the first argument and an instance of the first argument’s FuelType as the second argument.

The answer, the two traits below, was to give Car a representation type C <: Car[C]. The problem that I now have is that I can’t figure out how to define the type parameter on the concrete classes implementing Garage, e.g. ConcreteGarage below.

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)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:21:18+00:00Added an answer on June 7, 2026 at 6:21 am

    That’s why I said beware of the ugliness of having to carry around another type parameter for the rest of your life 🙂

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

    Of course, its simpler if you have a particular garage.

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up on a previous question I had ( Complexity of STL
This is a follow up to a previous question that I had before about
This is a follow-on from a previous question I had: How to decouple my
I suppose this question is a follow up to a previous question I had
This is a follow-up to a previous question I had about interfaces. I received
This is a follow up to a previous question which seems to have confused
This question is a follow up to a previous question I had I'm trying
This is a follow up question to a previous question I asked about calculating
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question . Suppose I am writing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.