Scala – Case Objects

Case Objects are singleton objects similar to an object with the modifier case. Similar to case classes a lot of boilerplate code is added to when the object is prefixed with the modifer case. Case objects are also serializable as is the case with case classes which is really useful when you are using frameworks like Akka. Defining case objects is simple. See Below Case Objects are quite similar to case classes. They can have members and have some of the same methods like toString, hashCode. Similarly, it is important to understand that some of the methods available in case classes are not available to case objects, for example, apply, unapply. Sealed Case Objects One of the variations of case objects is the sealed case objects. Sealed Case objects are an interesting alternative to the enumeration trait. If you want a peek into scala enumeration trait before diving into sealed case objects, … Read more

Scala – Pattern Matching & Case Classes

Pattern matching is also applied to case classes. Rather patter matching is always discussed along with case classes. Let’s see how we can apply pattern matching to case classes. Let’s look at an example. See Below In the above example, there is a Person case class defined and a match-case pattern matching function is defined which checks the lastName element of the Person class. If the lastName is Doe it prints that it has “Found a Doe!” and if it lastName is Cole it prints it has “Found a Cole!!”, otherwise, it prints “Unknown Last Name”. Let’s start to mix up – In one of the first blog entry of Pattern matching we had discussed Or clause in values. We can use it in case classes as well. See Below The result is whenever the lastName is Cole or Doe it will print “Found a Doe or Cole!”. Now let’s apply … Read more

Scala – Pattern Matching – Values, Types & Variables

Pattern matching in Scala is one of the most widely used and discussed topics. In simple terms, it is similar to the switch-case. Let’s use that familiarity to get a simple understanding of pattern matching and build up from there. Let’s first look at a simple match-case expression and see how it works. In the above example, we have a function called multiply, which takes a parameter and returns a value. If the value is 4 it will multiply the value by 3. Underscore _ acts as a catch-all for any other values and it will multiply by 2. This is simple. But what if we want to apply the same logic for more than one patterns. Writing individual case statements would make the code unnecessarily long. Let’s apply an OR logic to the expression. In the above example, we have a function called multiply, which takes a parameter and returns … Read more

Scala – Case Classes

Computing is all about logic and data. Various languages provide different ways to handle data. Primitive data types like Int, String and Boolean are usually never enough to represent anything meaningful by themselves. For example – If we were to capture the details of a Person – it will consist of many attributes. First Name, Last Name, Age, email Id etc. The real-world data is more complex and is made up of different primitive data types. Scala provides a clean way of putting and aggregating all this data together into immutable structures called case classes. Case classes are defined as normal classes except the definition is pre-fixed by a modifier case. Case classes provide a lot of boilerplate code, which reduces the amount of code which needs to be written to handle complex aggregated data. Cases classes are also very useful in pattern matching and if you are working with … Read more