I am a newbie to scala. So far i learned that, object in scala is singleton and if we declare case object, then override and hashcode default implementations are also added.
Just wondering to find any simple practical example, where we can fit case object.
Edit 1:
@Aivean :-
But without declaring object as case, below code also works fine :-
object ScalaPractice {
def main(args: Array[String]): Unit = {
val trade1 = Trade(EUR)
trade1.currency match{
case EUR | USD => println("trade possiblein this currency : " + trade1.currency)
case _ => println("trade not possible")
}
}
}
case class Trade(val currency : Currency){
}
sealed trait Currency { def name: String }
object EUR extends Currency { val name = "EUR" }
object USD extends Currency { val name = "USD" }
Why it is required to add case ?
Edit 2:
@dwickern
As @dwickern quoted :-
Converting your object to a case object gives you:
- A slightly nicer default toString implementation
- The case object automatically implements Serializable. This can be important, for example when passing these objects as messages via akka remoting
- You also get default equals, hashCode and scala.Product implementations just like a case class but these really don't really matter
Is there any official documentation for this on scala website, scal docs, etc.. (specially for the third point, that is in bold italics)