How to pattern match abstract parent classes in a inheritance tree

自作多情 提交于 2019-12-23 19:25:39

问题


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


来源:https://stackoverflow.com/questions/14802330/how-to-pattern-match-abstract-parent-classes-in-a-inheritance-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!