anindyaju99 anindyaju99 - 1 month ago 21
Scala Question

Scala: case classes converted to object

I have two case classes like these -

case class GetList()
case class Add(num: Int)


I have an actor whose receive method looks like this -

override def receive() = {
case a: Add => // do something
case g: GetList => // do something
case x: Any => // something
}


I am calling the actor as -

ref ! Add(1)
ref ! GetList


For the Add(1) message, I am getting a hit on a: Add in receive. But for the GetList message it's going to x: Any.
On closer inspection I found the instance I am getting is of type GetList$ which usually denotes an "object".

Who is doing this conversion and why? What is the rationale behind such a weird behaviour?

Answer

Your better off making GetList a case object instead so your new code would look like case object GetList and this way you don't have to worry about empty parenthesis. The other thing to note is that empty case classes have been deprecated as well.