Scala Case Class – How to Create Scala Object
Keeping you updated with latest technology trends, Join DataFlair on Telegram
1. Scala Case Class – Objective
In this Scala tutorial, we will see how to define a Scala case class. Moreover, we will also take a look at how to compare case classes and create shallow copies. Along with this, we will also learn how to create a Scala object.
So, let’s explore Scala Case Class and Scala Object.
2. What is Scala Case Class?
A Scala Case Class is like a regular class, except it is good for modeling immutable data. It also serves useful in pattern matching, 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.
Let’s Revise Scala Syntax with example.
3. 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. We can keep the parameter list empty.
So, let’s define a class ‘Song’.
scala> case class Song(title:String,artist:String,track:Int)
4. Creating a Scala Object
And now, it’s time to create a Scala Object for this Scala class.
scala> val stay=Song("Stay","Inna",4) stay: Song = Song(Stay,Inna,4)
To create a Scala Object of a case class, we don’t use the keyword ‘new’. This is because its default apply() method handles the creation of objects.
Let’s try accessing the title of this object:
scala> stay.title res1: String = Stay And now, let’s try modifying it. scala> stay.title="Me Gusta" <console>:12: error: reassignment to val stay.title="Me Gusta" ^
Explore Scala Closures with Examples | See What is Behind the Magic
So, this tells us that Scala case classes hold all vals, and this makes them immutable. We cannot reassign them. And while it is discouraged to do so, we can use vars in a case class.
5. Comparing two Case Classes
Now, let’s take a look at how to compare two Scala case classes.
scala> val crybaby=Song("Cry Baby","Melanie Martinez",7) crybaby: Song = Song(Cry Baby,Melanie Martinez,7) scala> val cry_baby=Song("Cry Baby","Melanie Martinez",7) cry_baby: Song = Song(Cry Baby,Melanie Martinez,7) They hold the same contents, but are they the same? Let’s ask Scala. scala> crybaby==cry_baby res2: Boolean = true Yes. Yes, they are.
6. Shallow-Copying a Scala Case Class
Since a Scala case class is immutable, we might sometimes need a copy to make changes in without changing the original. So, we now see how to create a shallow copy of it. But before that, let’s see a little about shallow and deep copies.
A deep copy is a copy to another object where any changes we make to it don’t reflect in the original object.
A shallow copy, however, is one where changes to the copy do reflect in the original.
So, Scala uses the method copy() to carry out a shallow copy.
scala> val chandelier1=chandelier.copy() chandelier1: Song = Song(Chandelier,Sia Furler,3)
It is also possible to change the constructor arguments.
scala> val chandelier2=chandelier.copy(title=chandelier.artist,artist="Sia") chandelier2: Song = Song(Sia Furler,Sia,3)
So, this was all about Scala Case Class and Scala Object. Hope you like our explanation.
7. Conclusion: Scala Case Class and Scala object
So, this is how we define a case class and process it. Hence, we saw Scala Case Class. Moreover, we discussed Creating a Scala object. Also, we compared two Scala Case Classes. Finally, we looked at shallow copying a Scala Case Class. Furthermore, feel free to leave your doubts and suggestions in the comment section.