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) ?
This is not only because
A with Bhas to be regarded as a new type. For the Scala type system, it does not directly matter if there exists a class corresponding toA 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
Aand all the methods fromB. 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:
Thas no method implementations, it creates an interface which defines all the methods in the trait.If the trait
Thas method implementations, it additionally creates a classT$classwhich has a static method for each of the concrete methods inT. This static method has the same body as its corresponding method inT, but its signature is changed to include thethisparameter. IfThad:then
T$classwill have:The class obtained by mixin composition of some class
Aand some traitTwill 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 inT. This is why the anonymous class has to be created – it has to have bridge methods defined for each method inT.Here’s an example. When you create a new class by doing mixin composition, e.g. call
new A with T:the compiler will rewrite it roughly to something like this:
Notice that the compiler could actually rewrite the callsites to
footo 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.