At Knoldus, we are endeavoring to learn Scala 3 and share it with community. There are multiple blogs which have been published here on Scala 3 which you can visit to get to know about other new features of the language. In this blog post, I will take you through the feature of trait parameters introduced in the language.
Traits in Scala 2
Traits are a powerful feature of Scala which help in structuring ADTs, reusing components and in preventing pattern matching errors (sealed traits). But in Scala 2, one thing that we cannot do with traits is to pass parameters to them. So, we would need to something as shown in the below example.
As the output clearly shows, though the quantity of buy1
is 100, when invoking printQuantity
it prints 0. This is called early initialization.
A potential workaround is early definitions which would look like as follows:
The solution is not very intuitive to many and becomes complicated quite often.
Scala 3 Trait Parameters
With Scala 3 introducing the feature of trait parameters, the solution to the above scenario becomes cleaner and easy to understand. Look at the following code from Scala 3 –
Of course, as traits can take parameters now and as we know that multiple traits can be mixed in hierarchy, this feature could have resulted in ambiguities. To prevent this, Scala has laid the following rules and anything other than that is treated as illegal –
- If a class C extends a parameterized trait T, and its superclass does not, C must pass arguments to T.
- If a class C extends a parameterized trait T, and its superclass does as well, C must not pass arguments to T.
- Traits must never pass arguments to parent traits.
The following is illegal in Scala 3.
This brings us to the end of this blog. I hope you got to learn something. Feel free to drop us a note in case you have questions. Do checkout more blogs written by our engineers.
References
https://dotty.epfl.ch/docs/reference/other-new-features/trait-parameters.html
https://docs.scala-lang.org/sips/trait-parameters.html
1 thought on “Introducing Trait Parameters in Scala 32 min read”
Comments are closed.