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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:28:17+00:00 2026-06-08T07:28:17+00:00

Because traits with representation types are self-referential, declaring that a variable holds an instance

  • 0

Because traits with representation types are self-referential, declaring that a variable holds an instance of that trait is a little difficult. In this example I simply declare that a variable holds an instance of the trait, declare that a function takes and returns and instance of that trait, and call that function with the variable:

trait Foo[+A <: Foo[A]]
case class Bar() extends Foo[Bar]
case class Grill() extends Foo[Grill]

// Store a generic instance of Foo
val b: Foo[_] = if(true) {
  Bar()
} else {
  Grill()
}

// Declare a function that take any Foo and returns a Foo of the same type
// that "in" has in the calling context
def echoFoo[A <: Foo[A]](in: A): A = in

// Call said function
val echo = echoFoo(b)

It fails with the error:

inferred type arguments [this.Foo[_$1]] do not conform to method 
echoFoo's type parameter bounds [A <: this.Foo[A]]
val echo = echoFoo(b)
           ^

Now, this makes sense because [_] is like Any (in ways I don’t fully understand). What it probably wants is something like Foo[Foo[_]], so that the type parameter conforms to the bounds of A <: Foo[A]. But now there’s an inner Foo that has a non-conforming type parameter, suggesting that the solution is something like Foo[Foo[Foo[Foo[..., which is clearly not correct.

So my question can probably be distilled down to: What is the Scala syntax for “This variable holds any legal Foo“?

  • 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-08T07:28:20+00:00Added an answer on June 8, 2026 at 7:28 am

    Self-referential type parameters like this are a bit problematic, because they’re not sound. For example, it’s possible to define a type like the following:

    case class BeerGarden extends Foo[Grill]
    

    As you can see, the A <: Foo[A] bound isn’t sufficiently tight. What I prefer in situations like this is to use the cake pattern, and abstract type members:

    trait FooModule {
      type Foo <: FooLike
    
      def apply(): Foo
    
      trait FooLike {
        def echo: Foo
      }
    }
    

    Now you can use the Foo type recursively and safely:

    object Foos {
      def echo(foo: FooModule#Foo) = foo.echo
    }
    

    Obviously, this isn’t an ideal solution to all the problems you might want to solve with such types, but the important observation is that FooLike is an extensible trait, so you can always continue to refine FooLike to add the members that you need, without violating the bound that the type member is intended to enforce. I’ve found that in every real-world case where the set of types I want to represent is not closed, this is about the best that one can do. The important thing to see is that FooModule abstracts over both the type and the instance constructor, while enforcing the “self-type.” You can’t abstract over one without abstracting over the other.

    Some additional information on this sort of thing (and a bit of a record of my own early struggles with recursive types) is available here:

    https://issues.scala-lang.org/browse/SI-2385

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

Sidebar

Related Questions

Because this is my first attempt at an extension method that seems quite useful
trait NotNull {} I've been trying to see how this trait can guarantee that
Because data from file look like this: line 1 is name (first last), next
I have this function that converts an integer to a std::string: std::string intToStr(const int
I'm trying to do something like this (completely synthetic example, because the real code
I have the following classes/traits in Scala trait Write[-T] { def add(elem : T);
Because shells other than ksh do not support pass-by-reference, how can multiple arrays be
Because my CSS3 animation uses over 50% of CPU in Chrome Browser 21.0 on
because when I use the function getUser () do not get the correct id?
Because it takes me a lot of time to integrate the Sherlock Action Bar

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.