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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:21:56+00:00 2026-06-06T19:21:56+00:00

After defining the following : abstract class A { type T def print(p: T)

  • 0

After defining the following :

abstract class A {
  type T
  def print(p: T) = println(p.toString)
}

trait B extends A {
  type T <: String
}

As expected, we can not create an object with T = Int :

scala> val a = new A with B {type T = Int}
<console>:9: error: overriding type T in trait B with bounds >: Nothing <: String;
 type T has incompatible type
       val a = new A with B {type T = Int}
                                  ^

As expected, we can create an object with T = String:

scala> val a = new A with B {type T = String}
a: A with B{type T = String} = $anon$1@692dec

scala> a.print("test")
test

After casting our value a to the type A with B, we get an error when calling the print method. It seems the type field T lost its information about the type (?).

scala> val b = a.asInstanceOf[A with B]
b: A with B = $anon$1@1927275

scala> b.print("test")
<console>:15: error: type mismatch;
 found   : java.lang.String("test")
 required: b.T
              b.print("test")
                      ^

Question 1: Why is the information about the type field T lost after the cast ?

Okay, so we try it again with a cast which explicitly sets the type field T to a String type:

scala> val c = a.asInstanceOf[A with B {type T = String}]
c: A with B{type T = String} = $anon$1@1927275

scala> c.print("test")
test

Okay, this works – good.

Now let’s try something crazy:

scala> val d = a.asInstanceOf[A with B {type T = Int}]
d: A with T{type T = Int} = $anon$1@1927275

scala> d.print(3)
3

Question 2: Huh ? Trait B restricted type T to be a subtype of String, but now the print method works with integers. Why is this working ?

  • 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-06T19:21:57+00:00Added an answer on June 6, 2026 at 7:21 pm

    Question 1 — “After casting our value a to the type A with B, we get an error when calling the print method.” What information about T is there after the cast? It is precisely what is in B:

    type T <: String

    Therefore the type is not known, just its upper bound. The following shows why the print call for A with B is forbidden:

    trait X
    trait Y extends X { def hallo() = () }
    
    trait A {
      type T
      def test(t: T) = ()
    }
    
    trait B extends A {
      type T <: X
    }
    
    val y = new A with B { type T = Y; override def test(t: T): Unit = t.hallo() }
    y.test(new X {})     // refused -- rightfully
    y.test(new Y {})     // ok
    
    val yc = y: A with B // note how we can cast type safe this way!
    yc.test(new X {})    // refused -- rightfully (would try to call inexistent 'hallo')
    

    So it’s a problem of what can happen to types occurring in contravariant (method argument) positions. Had you defined B by narrowing the lower bound, i.e. type T >: X, it would be possible to call test even if T was not fixed.


    Question 2 — Of course it works. You can make the compiler allow any call with the type casting. After you cast to A with B {type T = Int}, you force the compiler to accept that T = Int. Now the toString method you call is defined for java.lang.Object, and due to the generic structure of A, your Int is boxed into a java.lang.Integer, and therefore you do not witness any runtime problem when calling toString.

    But it is wrong to think that you are doing something correct here. For example:

    abstract class A {
      type T
      def print(p: T) = println(p.toString)
    }
    
    trait B extends A {
      type T <: String
      override def print(p: T) = println(p.toUpperCase) // !
    }
    
    val a = new A with B { type T = String }
    val b = a.asInstanceOf[A with B { type T = Int }]
    b.print(33)  // bang -- runtime class cast exception
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've downloaded memcache.php version 3.0.6 from there and out of the box (after defining
The following is a snippet from a xaml defining a DataGrid in a Control,
I currently have the following javascript code (loosely modeled after how a similar objective
UPDATE After lots of toiling, trouble and help from the community the following link
So I was reading the following documentation on defining your own property types in
I'm currently programming an OCaml module defining a type corresponding to a CPU register.
For example if I was defining the following function exprod[n_] := Expand[Product[x + i,
After upgrading all my Maven plugins version for a project, I encountered the following
Possible Duplicate: Making an undefined class as friend, and defining it later. I have
Why does the following throw a compiler error: class A { public: int f()

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.