Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scala have a case object?

Tags:

scala

It cannot be initialized with params and it will only be compared with itself (singleton). So no advantage for its equals and hash code function. Has anyone come across a case where they find it useful?

like image 991
Dragonborn Avatar asked Sep 16 '15 07:09

Dragonborn


People also ask

What is the use of case object in Scala?

A case object is like an object , but just like a case class has more features than a regular class, a case object has more features than a regular object. Its features include: It's serializable. It has a default hashCode implementation.

Why do we use case class in Scala?

The one of the topmost benefit of Case Class is that Scala Compiler affix a method with the name of the class having identical number of parameters as defined in the class definition, because of that you can create objects of the Case Class even in the absence of the keyword new.

What is case _ in Scala?

case _ => does not check for the type, so it would match anything (similar to default in Java). case _ : ByteType matches only an instance of ByteType . It is the same like case x : ByteType , just without binding the casted matched object to a name x .

What is the use of case class in spark?

Delta Lake with Apache Spark using Scala The case class defines the schema of the table. The names of the arguments to the case class are read using reflection and they become the names of the columns. Case classes can also be nested or contain complex types such as Sequences or Arrays.


2 Answers

You can use case objects as an alternative for enumerations.

Details can be found here: Case objects vs Enumerations in Scala


A simplified example from that question:

sealed trait Currency {
  def name: String
  def symbol: String
}

case object EUR extends Currency {
  val name = "EUR"
  val symbol = "€"
}

case object USD extends Currency {
  val name = "USD"
  val symbol = "$"
}

Advantages

  • This way there can be more fields (compared to ID and name in an Enumeration)
  • The compiler warns (in case of a sealed type hierarchy), if a match is not exhaustive.

So this code

val ccy: Currency = EUR
ccy match {
  case EUR =>
    println("Euro")
}

will result in

Warning:(27, 7) match may not be exhaustive.
It would fail on the following inputs: USD
ccy match {

Disadvantages

  • There is no "get by name" method (Enumeration provides withName())
  • You cannot iterate over "all" elements
like image 71
Beryllium Avatar answered Oct 02 '22 18:10

Beryllium


I think the most important difference is that case objects can be serialized while simple objects cannot.

This makes them very useful as messages with Akka-Remote.

EDIT: As Rüdiger Klaehn pointed out, this is not the only benefit we get from the case keyword. There is also:

  • hashCode implementation
  • a useful toString implementation

For classes additionally:

  • pattern matching optimization
  • a companion object with useful apply and unapply implementations

(This list may not be exhaustive!)

like image 30
Sascha Kolberg Avatar answered Oct 02 '22 17:10

Sascha Kolberg