
Scala has way too many unique features and to enable these unique features on JVM, scala performs various tricks behind the scene to do its job done.
In this blog, we are going to see one of the scala’s tricks for trait mixin at object creation time.
But before moving to trait mixin, let catch up with some easy examples:-
We all know scala’s traits are compiled into java’s interfaces and both cannot be initiated. The only way to use any one of them in their domain is by “Inheritance”, by creating a subclass we can initiate them, for example:-
Initiating using subclass: –
trait Calculator
class CalculatorExample extends Calculator
object Main extends App
{
val example : Calculator = new CalculatorExample
println(example)
}
OUTPUT: – “CalculatorExample@34f5a531”.
Explanation: –
classname@hashcode, where the class name is the name of the class whose object is created, and since here the reference is of type “Calculator” and object is of type “CalculatorExample”. Therefore we get the output as- “CalculatorExample@########”.
but that doesn’t mean we cannot initiate with “new” keyword: –
trait Calculator
object Main extends App
{
val example : Calculator = new Calculator {}
println(example)
}
OUTPUT: – “Main$$anon$1@498c2a19”.
From above we can see that by using “{}” with “new” keyword we can now able to initiate interface and trait. In this case, there is no violation of interface/trait property but there is a small trick that is performed by scala/java compiler.
Explanation: –
When we compile, the compiler creates a class named “classname$count” in java whereas “classname$anon$1” in scala, where the class name is the name of the class where the object is created that is in our example “Main” (in scala object are stored as “objectname$.class” after compilation) and the count is the number of times a new object is created.
Now this class is the subclass of “Calculator”. And is used to create an object and we get our output as – “Main$$anon$1@########”.
Trait Mixin
trait Calculator
trait Adder extends Calculator
trait Subtractor extends Calculator
trait Multiplier extends Calculator
trait Divider extends Calculator
object Main extends App
{
val example =
new Adder with Subtractor with Multiplier with Divider
println(example)
}
OUTPUT: – “Main$$anon$1@553cf826”.
In the above example, both Adder and Calculator are traits and hence cannot be initiated with a “new” keyword with one more point to notice is that there is also no “{}” after “new” keyword.
Explanation: –
Here again, the compiler does some behind the scene tricks to get it to work.
What happens here, Scala’s compiler again creates a class named classname$anon$count.class”, where the class name is the name of the class where the object is initiated that is here “Main” (in scala object are stored as “objectname$.class” after compilation) and the count is the number of time object is created.
And then use this class to instantiate.
“Main$$anon$1” is used to create the object and hence the output of the previous example will be like “Main$$anon$1@########”
This class implements all the traits (Adder, Subtractor, Multiplier, Divider) used for mixins and hence the subclass of all the traits.
Another Example –
trait Calculator
trait Adder extends Calculator
trait Subtractor extends Calculator
trait Multiplier extends Calculator
trait Divider extends Calculator
class CalculatorExample extends Calculator
object Main extends App
{
val example =
new CalculatorExample with Adder
with Subtractor with Multiplier with Divider
println(example)
}
OUTPUT: – “Main$$anon$1@4b15a6b”.
In this example “Main$$anon$1” class is a subclass of both CalculatorExample class and traits.
And the object of this class is used, this is how trait mixins are done.
You can use an online Java decompiler, for example,
http://www.javadecompilers.com/ to decompile .class file into .java file and can see what other tricks scala performs to provide its rich feature set.
Hope This Helps. For any queries, feel free to comment.
Stay Tuned for more.


