I am new to scala with a java background.
Is there a way to pattern match super classes (or traits) in a class inheritance tree with leafs as case classes and nodes abstract classes or traits? As far as I know case class inheritance is not allowed.
I think that pattern matching abstract classes in large inheritance tree would be very helpful
In the following code the last case in the match statement errors during compilation
sealed trait Person {
   def name: String
}
case class Customer(name: String, email: String) extends Person
sealed trait Employee extends Person {
   def id: Int
}
case class Worker(name: String, id: Int, skills: Array[String]) extends Employee
case class Manager(name: String, id: Int, title: String) extends Employee
def process(p: Person) = p match {
   case Customer(_, email) => email
   case Employee(name, _) => name + "@acme.com"
}
                You were missing the name field in Employee and an unapply method in the companion object of Employee:
sealed trait Employee extends Person {
  def id: Int
  def name: String
}
object Employee {
  def unapply(e: Employee): Option[(String, Int)] =
    Option(e) map { e =>
      (e.name, e.id)
    }
}
With the above changes the process method gives no problems:
def process(p: Person) = p match {
  case Customer(_, email) => email
  case Employee(name, _) => name + "@acme.com"
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With