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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:35:00+00:00 2026-06-11T00:35:00+00:00

OK, so, I’ve asked about this before. Ideally I’m looking for a general answer

  • 0

OK, so, I’ve asked about this before. Ideally I’m looking for a general answer that will help me understand how to specify types consistently, but in lieu of that, I’ll settle for how to solve specific problems. So far each solution seems to bring 3 more issues, I’m trying to avoid putting an entire application here but my goal is to find a way to refer to the type of a recursively parameterized trait type from anywhere, in a useful way, in a non-trivial program, where values of that trait type can be used interchangeably.

So, here’s more sample code:

//trait file, shouldn't need to know about implementing class.
trait MyTrait[T <: MyTrait[T]] { self:T =>
  val listOfT: List[T]
  def getFirst:T
  def getOne:T = if( !listOfT.isEmpty ) getFirst else self
}

case class Foo[A <: MyTrait[A]](i: MyTrait[A])

object MyTrait {
  def doSomething
      [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (t: U[T]): T = t.getFirst

  def testMethod1
      [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (something:Foo[T]):T=
              //error! type mismatch.  found:T, required: ?U[?T]
              doSomething(something.i.getOne)

  def testMethod2
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (something:Foo[T]):T=
        //error! type mismatch.  
        // found: something.i.type (with underlying type this.MyTrait[T]
        //required: T
        something.i

  def testMethod3
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (something:Foo[U[T]]):U[T]=
        //error: type arguments [U[T]] do not conform to class 
        //Foo's type parameter bounds [A <: this.MyTrait[A]]
        something.i.getOne


  // this works! ...but aren't something.i.getOne and something.i the same type?
  // why does testMethod2 fail if this works ?
  // what if I want to have a method that might return something.i and might return
  // soemthing.i.getOne?  What would the interface for that look like?
  def testMethod4
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (something:Foo[T]):T=
        something.i.getOne

  def testMethod5
       [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
      (something:Foo[U[T]]):U[T]=
        //error: type mismatch;
        //found: something.i.type (with underlying type this.MyTrait[U[T]]
        // required: U[T]
        something.i


}

//class file, shouldn't need to have MyTrait anywhere except 'extends' line.
//should be a usefull class on its own without adding the trait.
class MyClass extends MyTrait[MyClass] {
  //the point of using the parameterized type is to be able to return of 
  //List[MyClass] here instead of List[MyTrait] without having to override
  // some 'type' attribute in anything that uses the trait.
  override val listOfT: List[MyClass] = List[MyClass](this)
  override def getFirst: MyClass = listOfT.head
}


//some client code:
val mc = new MyClass
val foo = Foo(mc)
MyTrait.doSomething(foo.i)
//MyTrait.testMethod1(foo)

I figured out how to use the type parameter: [T <: MyTrait[T], U[X <: MyTrait[X]] <: MyTrait[X]]
from an answer to this question:
recursive type parameters in case class fields

and I’m basically asking the same thing again, but taking the problem a little further. You can see here that something.i basically has the same type as something.i.getOne, but those types can’t be used interchangeably, and so the object can’t be consistently used as a parameter to different functions here. How can I make this code work in a way that something.i and something.i.getOne (really probably even the same object) have the same type as recognized by the compiler and type system?

The meat of this particular question is in testMethod4 in the sample code.

  • 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-11T00:35:01+00:00Added an answer on June 11, 2026 at 12:35 am

    The pattern MyTrait[A <: MyTrait[A]] means you will want to work straight with A, and not anymore with MyTrait, as A extends this trait and guarantees to return instances of itself throughout the methods.

    Therefore, the mistake is the definition of Foo, it should be simply:

    case class Foo[A <: MyTrait[A]](i: A)
    

    With the given correction of Foo, your MyClass compiles and the ‘client code’, too.


    Furthermore, nesting the type like U[X <: MyTrait[X]] <: MyTrait[X]] does not make any sense. Eventually you will have one representation type. Method arguments are in contravariant position, so it totally suffices to have arguments of type T <: MyTrait[ T ] and you can stick in any representation type, no matter how specific. In other words, Foo[U[T]] doesn’t have any advantage over Foo[T] but makes things unnecessarily complicated. In all your test methods you can basically remove the U type parameters.

    object MyTrait {
      def doSomething[T <: MyTrait[T]](t: T): T = t.getFirst
    
      def testMethod1[T <: MyTrait[T]](something: Foo[T]): T =
        doSomething(something.i.getOne)
    
      def testMethod2[T <: MyTrait[T]](something: Foo[T]): T = something.i    
      def testMethod3[T <: MyTrait[T]](something: Foo[T]): T = something.i.getOne
      def testMethod4[T <: MyTrait[T]](something: Foo[T]): T = something.i.getOne
      def testMethod5[T <: MyTrait[T]](something: Foo[T]): T = something.i
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.