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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:09:42+00:00 2026-06-11T10:09:42+00:00

Given this code: trait S { def s: String } trait I { def

  • 0

Given this code:

  trait S {
    def s: String
  }

  trait I {
    def i: Int
  }

  case class CC_S(s: String) extends S
  case class CC_I(i: Int) extends I
  case class CC_S_I(s: String, i: Int) extends S with I

  def combine(s: S, i: I): S with I = CC_S_I(s.s, i.i)

This test succeeds:

Assert.assertEquals(CC_S_I("s", 1), combine(CC_S("s"), CC_I(1)))

(How) can I make the combine method generic as a trait with type parameters? I.e. I would like to define something like this:

  trait MyTrait[A,B] {
    def combine(a: A, b: B): A with B
  }

So that I can use it like this:

  class MyClass[A <: CC_S, B <: CC_I] extends MyTrait[A,B] {
    override def combine(s: A, i: B) = CC_S_I(s.s, i.i) //Note: this line does not compile...
  }

  Assert.assertEquals(CC_S_I("s", 1), new MyClass().combine(CC_S("s"), CC_I(1)))

Update. This would have been a better example:

  class MyClass[A <: S, B <: I] extends MyTrait[A,B] {
    override def combine(s: A, i: B) = CC_S_I(s.s, i.i) //Note: this line does not compile...
  }

See comments under Travis’s answer to see that answered as well.

  • 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-11T10:09:43+00:00Added an answer on June 11, 2026 at 10:09 am

    The problem is just that CC_S_I isn’t actually a subtype of either CC_S or CC_I (although it is a subtype of S and I). The following will work:

    class MyClass extends MyTrait[S, I] {
      def combine(s: S, i: I): S with I = CC_S_I(s.s, i.i)
    }
    

    As a side note, the Shapeless library is worth looking into if you’re doing much of this kind of thing. It allows you to write your combine method very generically:

    import shapeless._
    
    def combine[A, B, C, AL <: HList, BL <: HList, CL <: HList](a: A, b: B)(implicit
      aIso: Iso[A, AL],
      bIso: Iso[B, BL],
      cIso: Iso[C, CL],
      p: PrependAux[AL, BL, CL]
    ): C = cIso from p(aIso to a, bIso to b)
    

    Now you just need a bit of boilerplate:

    implicit val ccsIso = Iso.hlist(CC_S.apply _, CC_S.unapply _)
    implicit val cciIso = Iso.hlist(CC_I.apply _, CC_I.unapply _)
    implicit val ccsiIso = Iso.hlist(CC_S_I.apply _, CC_S_I.unapply _)
    

    And you can write:

    scala> val x = combine[
         |   CC_S, CC_I, CC_S_I, String :: HNil, Int :: HNil, String :: Int :: HNil
         | ](CC_S("s"), CC_I(1))
    x: CC_S_I = CC_S_I(s,1)
    

    The enormous type annotation is unfortunately necessary with this formulation, but this should give you some idea of what’s possible, and there are things you could do to make the usage cleaner.

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

Sidebar

Related Questions

Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have a domain model that looks like this: case class Account(id: Int, name:
I've come across a strange problem with method specialisation. Given this code... #include <string>
Given the following code: class A { class B type C <: B trait
Given a Map[String, Int] in Scala, I want to create a trait that allows
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
So I was given this code in a folder of bits and pieces that

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.