How to pattern match abstract parent classes in a

2019-07-02 06:37发布

问题:

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"
}

回答1:

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"
}


回答2:

It's not that fancy (as others may suggest with structural types,) but it's quite practical, I think:

def process(p: Person) = p match {
   case Customer(_, email) => email
   case e: Employee => e.name + "@acme.com"
}

If you need to match against some particular known value you may use guards:

def process(p: Person) = p match {
   case Customer(_, email) => email
   case e: Employee if e.name == "Joe" => e.name + "@acme.com"
}