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 8157445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:17:14+00:00 2026-06-06T17:17:14+00:00

I have two traits, each with a type parameter for one of its members.

  • 0

I have two traits, each with a type parameter for one of its members. In the first trait, I have a function that takes an instance of the second trait and an instance of the second trait’s type member. This function calls a function in the second trait that is expecting that instance of its type member. However, I cannot figure out how to properly parameterize the call so that this actually works. Here is a simplified example that fails:

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

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

  def Refuel(car: CarType, fuel: CarType#FuelType): Garage = {
    val car_index = Cars.zipWithIndex.find(_._1 == car).get._2

    copy(Cars.updated(car_index, car.Refuel(fuel)))
  }
}

trait Car {
  type FuelType <: Fuel
  def Fuel: FuelType

  def copy(Fuel: FuelType): Car

  def Refuel(fuel: FuelType): Car = {
    copy(fuel)
  }
}

trait Fuel

This fails with the following error:

error: type mismatch;
 found   : fuel.type (with underlying type Garage.this.CarType#FuelType)
 required: car.FuelType
    copy(Cars.updated(car_index, car.Refuel(fuel)))
                                            ^

How do I constrain the Garage.Refuel function so that it accepts a Car and any Fuel that is acceptable to that type of Car?

  • 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-06T17:17:16+00:00Added an answer on June 6, 2026 at 5:17 pm

    Although Daniel’s answer works, I would like to point out an alternative, which is kind of my own panacea. I had been struggling a lot with getting path dependent types right, and ended up with the following strategy. It’s a bit more ‘ugly’ as you now need to write an additional type parameter, but this approach has never let me down:

    trait Garage {
      type CarType <: Car[CarType]  // CarType appears as representation type on the right
      def cars: Seq[CarType]
    
      def copy(Cars: Seq[CarType]): Garage
    
      def refuel(car: CarType, fuel: CarType#FuelType): Garage = copy(
        cars.map {  // map is more concise for what you try to achieve
          case `car` => car.refuel(fuel)  // backticks to find the particular car
          case other => other
        })
    }
    
    trait Car[C <: Car[C]] {  // add a 'representation type'
      type FuelType <: Fuel
      def fuel: FuelType
    
      // use 'C' instead of 'Car' everywhere, and qualify the type member with 'C#'
      def copy(fuel: C#FuelType): C
    
      def refuel(fuel: C#FuelType): C = copy(fuel)
    }
    
    trait Fuel
    

    I don’t know if this ‘representation type’ concept has a formal name (I would be interested to know). I tried to look up who taught me this, but didn’t find it (at least in stackoverflow).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two application that need to talk to each other. App1 needs to
Say I have two classes with the same identifier for a parameterized type trait
Let's suppose I have a trait with two type parameters, e.g. trait Qux[A, B]
Say I have a trait that has two lists. Sometimes I'm interested in the
Say I have two traits that I would like to mixin to a class.
Or I have two files, each with one global initialization. One depends on the
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two DropDownListBoxes one is called ddlDay and the other is ddlMonth. As
I have two controls on a page, one is a search entry and submission
We have two servers in the same network. One of them is the server

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.