abstract class Animal
case class Cat(name: String) extends Animal
case class Dog(name: String) extends Animal
Say I have defined Cat and Dog, two
My guess is that this is an optimisation scalac performs. The unapply
method is synthetic, so the compiler knows its implementation and might improve on the runtime performance.
If that theory is correct, the following should be different:
object Cat {
def unapply(c: Cat): Option[String] = Some(c.name)
}
class Cat(val name: String) extends Animal