Scala – Enumerations

Enumerations represent meaningful descriptions for more primitive data types. Scala Enumeration trait can be used to define a finite set of values. Let’s see a simple declaration and understand what it all means The first line is extending the Enumeration trait The second line is a declaration of type alias so that it makes the code a bit more meaningful instead of just saying Value – for example declaring a function In the third line – at the end, it says Value, which is a method and not the type Value mentioned in the second line. This method returns a type Value. Each element of an enum has a unique running id, which is auto-incremented automatically at the time of assignment. Specific values can also be assigned the elements in scala enumerations. See Below Enumerations can be passed to functions. However, there are a couple of issues, if values other than the ones in … Read more

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 Traits

Traits in Scala are similar to interfaces in Java. However, they offer substantial enhancements to Java interfaces. They can also be used in Java classes under certain conditions. Traits encapsulate methods and fields which can be reused and mixed with classes. Scala allows both abstract and concrete methods and variables, unlike Java interfaces which have only abstract methods. So in some sense traits are like similar to classes except that it has a keyword trait in front of it when it is defined. Let’s define a trait which has two abstract variables and one abstract method. See Below Extending Traits Traits can be extended to create new traits. See Below While extending traits it is not necessary to implement the abstract methods. It is also possible to create traits with a mix of abstract and concrete methods which was not possible in Java. See Below Extending Classes with Traits Once … 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