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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:11:53+00:00 2026-05-26T08:11:53+00:00

I have the following nested structure involving type parameters and type members: trait B

  • 0

I have the following nested structure involving type parameters and type members:

trait B

trait B1 extends B

trait U {
  type _B <: B
}

type U1 = U {
  type _B = B1
}

class Q[_U <: U] {
  override def toString() : String = {
    // print out type information on B here...
  }
}

def test() {
  val q = new Q[U1]()
  println(q.toString())
}

It seems impossible to me gathering the type information on B at runtime because of the way U1 is specified.

Am I wrong? If not, is there a solution with minor changes in the setup?

Thanks to the answer from Kipton Barros I came up with the following setup:

trait B

trait B1 extends B
trait B2 extends B

trait U {
  type _B <: B
  implicit val mfB : Manifest[_B]
}

class U1 extends U {
  type _B = B1
  val mfB : Manifest[_B] = implicitly
}

class U2 extends U {
  type _B = B2
  val mfB : Manifest[_B] = implicitly
}

class Q[_U <: U](u : _U) {
  override def toString() : String = {
    "B: " + u.mfB.erasure.getName()
  }
}

def test() {
  println(new Q(new U1) toString)
  println(new Q(new U2) toString)
}

The only downside of this approach is the need for instantiation of U.

  • 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-26T08:11:54+00:00Added an answer on May 26, 2026 at 8:11 am

    I would have thought to use a combination of a type refinement and a Manifest. The former allows to surface the abstract type _B as a type parameter B, and the latter instructs the Scala compiler to reify the type of B (edit: the static type from the call context) as a run-time object. Here’s my attempt,

    trait B
    trait B1 extends B
    trait B2 extends B
    trait U { type _B <: B }
    class U1 extends U { type _B = B1 }
    
    class Q[B: Manifest, _U <: U { type _B = B}](u: U) {
      override def toString() : String = {
        implicitly[Manifest[B]].toString // Manifest[B] was an implicit parameter to Q
      }
    }
    
    // Four tests:
    println(new Q[B1, U1](new U1) toString)        // (1) prints "$line1.$read$$iw$$iw$B1"
    // println(new Q[B2, U1](new U1) toString)     // (2) correctly fails to compile
    // println(new Q[Nothing, U1](new U1) toString)// (3) correctly fails to compile
    println(new Q(new U1) toString)                // (4) prints "Nothing" (why not B1?)
    

    It works in the first case, where explicit type parameters are given. The second case correctly fails to compile, since U1 contains a B1 type, not a B2 type. Similarly for the third case. For some reason, though, the Scala compiler is generating an incorrect manifest in the fourth case, even though the compiler seems to infer type B1. I don’t know enough to say whether this is a bug, but it’s certainly surprising to me. Can anyone explain why case (4) doesn’t print B1’s Manifest?

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

Sidebar

Related Questions

I have to create the following type of HTML structure: <div id=dcontent> <ul class=thm>
Hi currently I have a nested XMl , having the following Structure : <?xml
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>
Assume the following: we have class B, which is a private class nested inside
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following file structure (XML files 'index.xml' in nested folders): index.xml foo/index.xml
I have a structure with the following domain classes: class Service{ static hasMany=[serviceRequirements:ServiceRequirement]} And
I have a query that produces the following output in a nested set structure:
I have the following DOM structure: /*:DOC += <div id='testDiv' class='testDivClass'><div id='innerTestDiv'></div></div><span id='testSpan' class='testSpanClass'><span
I have following structure: class User < ActiveRecord::Base has_many :Hobbies, :dependent => :destroy accepts_nested_attributes_for

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.