Traits as mix-ins
The way we do trait mix-ins is no different than inheriting any class in Scala; the only difference is that you can mix-in more than one trait and for that we have this nice keyword called with
. Why do we call it mix-in? We could have called it something else. Well, yes but this explains almost everything you can do with traits. It's easy to modify or add up behaviors to an already existing functionality or construct without affecting already existing behavior. We'll see that in a bit. Traits can be used in a variety of use cases such as:
- Composable mix-ins; to make already existing interfaces richer
- Stackable modifications
Traits as composable mix-ins
By composable mix-ins we mean that we can create an instance of a particular type, with mix-ins of a trait, that can have certain additive functionalities. If you're thinking why would we want to do that, then the answer is maybe you want to add some particular behavior that makes sense to your functionality and you want it to...