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

  • Home
  • SEARCH
  • 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 8166663
In Process

The Archive Base Latest Questions

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

scala> class A defined class A scala> trait B defined trait B Creating an

  • 0
scala> class A
defined class A

scala> trait B
defined trait B

Creating an object of class A gives us:

scala> new A
res4: A = A@11ea3fc

But creating an object of class A with trait B mixed in gives us:

scala> new A with B
res3: A with B = $anon$1@172aa3f

Here we have an anonymous class (hinted by anon). Why ?

Is this because the type A with B is regarded as a new type (and which was not defined with an identifier before) ?

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

    This is not only because A with B has to be regarded as a new type. For the Scala type system, it does not directly matter if there exists a class corresponding to A with B. An anonymous class is generated because it has to contain bridge methods for all methods in the traits that have been mixed in.

    The reason that an anonymous class is created is that the object must have implementations of all the methods from A and all the methods from B. On the JVM bytecode level, this would warrant inheriting multiple classes, and the multiple inheritance model is not supported on the JVM.

    To simulate multiple inheritance (or mixin composition, however you wish to call it), Scala does the following things when you create a trait:

    1. If the trait T has no method implementations, it creates an interface which defines all the methods in the trait.
    2. If the trait T has method implementations, it additionally creates a class T$class which has a static method for each of the concrete methods in T. This static method has the same body as its corresponding method in T, but its signature is changed to include the this parameter. If T had:

      def foo(x: Int) = x
      

    then T$class will have:

    <static> def foo($this: T, x: Int) = x
    

    The class obtained by mixin composition of some class A and some trait T will then have a special bridge method generated which forwards the call to the static method which contains the body. This way, the body of the method is not duplicated in every class which mixes in T. This is why the anonymous class has to be created – it has to have bridge methods defined for each method in T.

    Here’s an example. When you create a new class by doing mixin composition, e.g. call new A with T:

    class A {
      def bar = println("!")
    }
    
    trait T {
      def foo(x: Int) = x
    }
    
    new A with T
    

    the compiler will rewrite it roughly to something like this:

    class A {
      def bar = println("!")
    }
    
    <interface> T {
      def foo(x: Int): Int
    }
    
    class T$class {
      <static> def foo($this: T, x: Int) = x
    }
    
    class $anon extends A <implements> T {
      // notice that `bar` is inherited, but `foo` is not
      <bridge> def foo(x: Int) = T$class.foo(this, x)
    }
    new $anon
    

    Notice that the compiler could actually rewrite the callsites to foo to call the static methods directly from the callsite, rather than through a bridge method. The reason why it’s not done that way is because then it would not support subtyping polymorphism anymore.

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

Sidebar

Related Questions

scala> class A defined class A scala> trait T extends A { val t
scala> class A(implicit a: Int); defined class A scala> class B()(implicit a: Int); defined
In a Scala Compiler Plugin, I'm trying to create a new class that implement
I'm using Scala 2.9.1 I've defined a Logging trait as such: trait Logging {
I'm aware that case class inheritance is deprecated in Scala, but for the sake
Why don't the following work? scala> abstract class Foo[B<:Foo[B]] defined class Foo scala> class
In scala 2.9.0.RC3, I defined a trait for parsers and a concrete example of
scala> import java.util.Properties import java.util.Properties scala> trait Foo extends Properties defined trait Foo scala>
I'm currently wondering about composing an object/class/trait that conforms to one trait for multiple
scala> class A { type T <: String; def f(a: T) = println(foo)} defined

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.