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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:42:04+00:00 2026-05-26T19:42:04+00:00

Say I have two classes with the same identifier for a parameterized type trait

  • 0

Say I have two classes with the same identifier for a parameterized type

  trait A {
    type O
    def f(a: Int, os: Seq[O]): Int
  }

  trait B {
    type O
    def g(a: Int, o: O): Int = { h1(o) + h2(a) }
    def h1(o: O): Int
    def h2(a: Int): Int = {a/2}
  }

I would like to create a child class that will “marry” the two

  trait C extends A with B {
    def f(a: Int, os: Seq[O]): Int = {
      os.map{ o => g(a, o) }.sum
    }
  } 

Finally, I create an implementation for C

  class D extends C {
    type O = Int
    def h1(o: O): Int = {5 * o}
  }

At writing of C I don’t yet know what type O is — however, I’d like to constrain A.O == B.O such that it “makes sense” to use B.g in an implementation of A.f. I tried implementing this and it surprisingly seemed like Scala assumed there was only ever one type O

  val d = new D
  println(d.f(1, Seq(1,2,3)))

To me this seems incorrect — why should A.O and B.O agree?

EDIT
I’d also like to note that If you were to instead put constraints on O like so,

  case class Z()
  case class Z1() extends Z
  case class Z2() extends Z1

  trait A {
    type O <: Z
  }

  trait B {
    type O >: Z2
  }


class D extends C {
    type O = Z1

Compilation will fail. However, if you put this instead,

  trait A {
    type O <: Z
  }

  trait B {
    type O <: Z1
  }
  class D extends C {
    type O = Z2

Compilation succeeds and everything runs fine.

  • 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-05-26T19:42:04+00:00Added an answer on May 26, 2026 at 7:42 pm

    I think Scala always “marries” the members — both type and value members — of traits when both are mixed in:

    trait A { type T = Int }
    trait B { type T = String }
    
    object C extends A with B
    

    gives

    overriding type T in trait A, which equals Int;
    type T in trait B, which equals String needs `override’ modifier

    (it’s how Scala deals with the multiple inheritance issue — no matter how many times an identifier is mixed in, it only exists once).

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

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
Let’s say you wish to have two classes like so, each declaring an instance
Let's say I have two classes. Both have the same member fields (including the
Say I have two different cpp files. Both declare classes with the same name,
Say you have these two classes. public class Author { public int ID {get;
Say I have two classes and have a requirement that the primary key property
Say you have two classes, A and B. Is it possible to instantiate both
Say I have two classes created work and workItem. CWorker *work = new CWorker();
Say I have two classes, and neither of them are GUI components. Class A
Let's say we have two classes, Foo and Foo Sub, each in a different

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.