Difference between case class and case object?

ScalaAkka

Scala Problem Overview


I am learning Scala and Akka and in my recent lookup for a solution, I found something like

 case class TotalTaxResult(taxAmount:Double)
 case object TaxCalculationTimeout

What is the difference between the two?

When should I use one over the other?

Scala Solutions


Solution 1 - Scala

A case class can take arguments, so each instance of that case class can be different based on the values of it's arguments. A case object on the other hand does not take args in the constructor, so there can only be one instance of it (a singleton, like a regular scala object is).

If your message to your actor does not need any value differentiation, use a case object. For instance, if you had an actor that did some work, and you, from the outside, wanted to tell it to do work, then maybe you'd do something like this:

case object DoWork

...

def receive = {
  case DoWork => 
     //do some work here
}

But if you wanted some variation in how the work is done, you might need to redefine your message like so:

case class DoWorkAfter(waitTime:Long)

...

def receive = {
  case class DoWorkAfter(time) =>
    context.system.scheduler.scheduleOnce(time.milliseconds, self, DoWork)

  case DoWork => 
     //do some work here
}

Solution 2 - Scala

A case object is a singleton case class. They are used kind of like enumeration values. It can be used in pattern matching just like any other value:

TaxCalculationTimeout match {
  case TaxCalculationTimeout => println("hello")
}

When you define a case class, you are creating a template for instances of that class. TotalTaxResult(1.0) and TotalTaxResult(2.0) are two different values of the same type. Whereas there is exactly one TaxCalculationTimeout value.

Solution 3 - Scala

In simple words, Scala is a Object Oriented and Functional programming language. It have features of functional programming like pattern matching with pure object oriented methodology.

Some times, we need to create singleton object without any value like passing some signal for pattern matching. If scala have not concept of case object we just to need to use enum or equals some string value in matching. But this is not a readability in pure Object Oriented language.. In that scenario we are using Case Object

Case classes are used when we need to create multiple objects with different values.

Solution 4 - Scala

You can think of these two to be just like a class and an object in general. When you do a case class ClassName(params list) it creates a blueprint for making objects and case object defines a singleton object in the scope in which it is declared. Starting with Scala 2.10, you should always use case objects instead of case classes with no arguments. So when you want to do a pattern match on some values which need arguments you should go for a case class, but if your values don't take arguments then you should use a case object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaydreamerView Question on Stackoverflow
Solution 1 - ScalacmbaxterView Answer on Stackoverflow
Solution 2 - ScalagregghzView Answer on Stackoverflow
Solution 3 - ScalaHarmeet Singh TaaraView Answer on Stackoverflow
Solution 4 - ScalaPrashant_SharmaView Answer on Stackoverflow