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 behave as if it has come from the library itself. As a client of such constructs or libraries, we want them to feel as natural as they are. Traits help us add certain functions, keeping the real library intact.

We can understand this better with the help of an example. So, suppose you have an already existing CreditCard functionality. For demonstration purposes, we'll limit our expectations ...

Get Learning Scala Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.