Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pattern match abstract parent classes in a inheritance tree

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"
}
like image 269
Kostas Avatar asked Dec 26 '22 09:12

Kostas


1 Answers

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"
}
like image 94
EECOLOR Avatar answered May 09 '23 03:05

EECOLOR