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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:01:32+00:00 2026-05-25T03:01:32+00:00

If I have the following Scala type hierarchy: // Base traits trait TA[X <:

  • 0

If I have the following Scala type hierarchy:

// Base traits
trait TA[X <: TA[X,Y], Y <: TB[X,Y]]
trait TB[X <: TA[X,Y], Y <: TB[X,Y]]
trait TC[X <: TA[X,_]]

// More specific traits
trait TI[X <: TI[X,Y], Y <: TJ[X,Y]] extends TA[X,Y]
trait TJ[X <: TI[X,Y], Y <: TJ[X,Y]] extends TB[X,Y]
trait TK[X <: TI[X,_]] extends TC[X]

// Concrete class that should implement TK, but does not compile
class Z extends TK[TI[_,_]]

// What is needed to be able to get class Z to compile
// The reference of X to itself has been removed.
trait TC2[X <: TA[_,_]]
trait TK2[X <: TI[_,_]] extends TC2[X]
class Z2 extends TK2[TI[_,_]]

TC would be a generic manager of some sort of TA.

TK would be a more specific manager of a more specific TA (TI).

Z would be the concrete implementation that manages any object implementing TI.

Z is not legal, but Z2 is. Unfortunately, TC and TK are more specific than TC2 and TK2. So is there a way of declaring Z using TC and TK, instead of TC2 and TK2?

[EDIT] I failed to say in my original question, that I kind of understand why Z is unsound. What I really want to know is, if there is a way of saying something like:

class Z3 extends TK[TI[TI,_]]
  • 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-25T03:01:32+00:00Added an answer on May 25, 2026 at 3:01 am

    When you have complex mutually recursive type bounds it can often be helpful see if you can translate your problem into an equivalent one using abstract type members instead. If we do that mechanically, your base and more specific traits end up looking like,

    // Base traits
    trait TA {
      type X <: TA
      type Y <: TB
    } 
    trait TB {
      type X <: TA
      type Y <: TB
    } 
    
    trait TC {
      self =>
      type X <: TA { type X <: self.X }
    } 
    
    // More specific traits
    trait TI extends TA {
      type X <: TI
      type Y <: TJ
    } 
    
    trait TJ extends TB { 
      type X <: TI
      type Y <: TJ
    }
    
    trait TK {
      self =>
      type X <: TI { type X <: self.X }
    } 
    

    and now we have a straightforward definition of Z as,

    class Z extends TK {
      type X = TI
    }
    

    Note that the definitions of the members of TA, TB and TI, TJ are essentially the same. Because these types are now type members we can factor them out into common base types like so,

    // Base traits
    trait T0 {
      type X <: TA
      type Y <: TB
    } 
    trait TA extends T0
    trait TB extends T0
    
    trait TC {
      self =>
      type X <: TA { type X <: self.X }
    }
    
    // More specific traits
    trait T1 extends T0 {
      type X <: TI
      type Y <: TJ
    }
    
    trait TI extends TA with T1
    trait TJ extends TB with T1
    
    trait TK extends TC {
      self =>
      type X <: TI { type X <: self.X }
    }
    
    class Z extends TK {
      type X = TI
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala I have the following trait: trait Reactive { type Condition = ()
I have the following classes/traits in Scala trait Write[-T] { def add(elem : T);
I have the following scala class definition (found in a paper), modeling categories: trait
I have the following Scala code: class X[T1 <: AnyRef] { var _x :
In Scala IDE I have the following code: package pkg.one object S { class
I have the following Scala code. import scala.actors.Actor object Alice extends Actor { this.start
Say we have the following two traits: trait Foo[A] { def howMany(xs: List[A]) =
I have the following code in Scala Play Framework: case class Step(name: String, f:
I have a beginners Scala question. I have a class, Sample which extends the
I have the following code: trait CellT[VAL <: AnyVal] { var value: VAL type

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.