Scala implicits are a great tool to remove code duplication and convert objects from different domains. However, we need to learn not to abuse them: by hiding code they can make the code more concise but more cryptic at the same time. In order to use implicits efficiently, we need to know how the compiler tries… Continue reading Implicits: Rules and Applications
Sometimes we want to hide/protect our variables — the classic concept of encapsulation in object oriented programming. We’d like to write a Scala class to manage someone’s age. In particular, we’d like to: - lie on our age if we are not teens anymore (you never know!) - put some validation to avoid negative age values Our… Continue reading Stateful objects: use them to lie!
Scala uses access modifiers quite differently from Java. Let’s see what the differences are and why qualified access modifiers are so powerful in Scala. default access modifier Scala >> public Java >> protected While in Scala the public keyword doesn’t exist, Java allows you to explicitly use the keyword protected as you please. private Scala >> accessible from a class and its companion… Continue reading Super Powers to Qualified Access Modifiers
Sometimes code is common to all the classes of a specific package (e.g.: conversions between objects, custom logs, shared operations, etc). Our lovely Java doesn’t seem to have a convenient way of declaring code as part of a package, rather than just a class….but FEAR NO MORE! Let’s see how we can this in Scala using package objects.… Continue reading Shared package code? Fear no more!
One of the most powerful (and cool) features in Scala is that you can use traits as stackable modifications. Let’s see how it works with a simple tutorial 🙂 Our goal is to create a list of ordered colours, let’s call it ColourMixer. First we create a class to represent what a basic (empty) colour mixer is: Let's create our Legos… Continue reading Traits are better than Legos!