Case classes and Anonymous Function in Scala

Knoldus Blog Audio
Reading Time: 3 minutes

In this blog, we’ll be looking at one of the important concepts of Scala that is Case class, also we will see how to define a Scala case class and along with this, we’ll discuss what are the important features of case classes, the importance of case classes. We will learn how to define a minimal case class and why do we need case classes in scala, understanding case class with the help of an example. Along with this will also learn about some important concepts of the Anonymous function in Scala.

What is a Case class?

A Scala Case Class is like a regular class, except it is good for modeling immutable data. In the mean time it also serves useful in pattern matching, However such a class has a default apply() method which handles object construction. A Scala case class also has all vals, which means they are immutable.

Features and advantages of Case class:-

  • Scala case classes are just regular classes which are immutable by default and decomposable through pattern matching.
  • All the parameters listed in the case class are public and immutable by default.
  • It uses equal method to compare instance structurally. It does not use new keyword to instantiate object.
  • Scala’s pattern matching statement is most useful for matching on algebraic types expressed via case classes.
  • Case classes are generally serializable by default.
  • Case classes have companion object.
  • They have handy copy methods as a result copy method creates a new instance of this case class.
  • Case classes are very easy to use in Pattern Matching.
  • They ease the development process and improve Productivity.
  • An apply method is created in the companion object of the class, so you don’t need to use the new keyword to create a new instance of the class.

Defining a Minimal Case Class in Scala:-

To define a minimal Scala Case Class we need the keywords ‘case class’, an identifier, and a parameter list. Although We can keep the parameter list empty.
So, let’s define a case class ‘Car’.

case class Car(company:String, model:String, price:Int)

Scala Case Class Example:-

case class Scala(a:Int, b:Int)  

object hyScala{  

 def main(args:Array[String]){  

        var c =  Scala(50,10)       // Creating object of case class  

        println(“a = “+c.a)               // Accessing elements of case class  

        println(“b = “+c.b)  

    } 

  

Importance of Case class and why do we need it?

 A case class has all of the functionality of a regular class, and more. Generally when the compiler sees the ‘case’ keyword in front of a class, it generates code for you, with the following benefits:

  • Case class constructor parameters are public val fields by default, so accessor methods are generated for each parameter.
  • An ‘apply’ method is created in the companion object of the class, hence you don’t need to use the new keyword to create a new instance of the class.
  • An ‘unapply’ method is generated, which lets you use case classes in more ways in match expressions.
  • copy method is generated in the class. You may not use this feature in Scala/OOP code, but it’s used all the time in Scala/FP.
  • ‘equals’ and ‘hashCode’ methods are generated, which let you compare objects and hence easily use them as keys in maps.
  • A Default ‘toString’ method is generated, which is further helpful for debugging.

Anonymous Function in Scala:-

In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. Moreover an anonymous function provides a lightweight function definition. It is useful when we want to create an inline function.

Features of Anonymous Functions:-

  • You can write anonymous functions as little snippets of code
  • You can use them with methods on the List class like map and filter
  • With these little snippets of code and powerful methods like those, also you can create a lot of functionality with very little code.

Syntax:-

(a:Int, b:Int)=> a*b
Or
(_:Int)*(_Int)
  • In the above first syntax, ‘=>’ is known as a transformer. The transformer is used to transform the parameter-list of the left-hand side of the symbol into a new result using the expression present on the right-hand side.
  • In the above second syntax, _ character is known as a wildcard is a shorthand way to represent a parameter who particularly appears only once in the anonymous function.

Conclusion:-

In this blog, we’ve taken a closer look at Case classes and Anonymous functions in Scala. We’ve described the benefits they give. However, they can serve pretty well as simple data containers due to the significant reduction of boiler-plate code.

References:-

https://docs.scala-lang.org/tour/case-classes.html
https://docs.scala-lang.org/overviews/scala-book/anonymous-functions.html

Leave a Reply