匹配 Java 接口时的 Scala match/case 语句

发布于 2024-11-05 02:21:56 字数 1105 浏览 0 评论 0原文

我使用 Scala match/case 语句来匹配给定 java 类的接口。我希望能够检查一个类是否实现了接口的组合。我似乎可以让它工作的唯一方法是使用嵌套的 match/case 语句,这看起来很难看。

假设我有一个 PersonImpl 对象,它实现了 Person、Manager 和 Investor。我想看看 PersonImpl 是否同时实现了 Manager 和 Investor。我应该能够执行以下操作:

person match {
  case person: (Manager, Investor) =>
    // do something, the person is both a manager and an investor
  case person: Manager =>
    // do something, the person is only a manager
  case person: Investor =>
    // do something, the person is only an investor
  case _ =>
    // person is neither, error out.
}

案例人员:(经理,投资者)不起作用。为了让它工作,我必须做以下看起来丑陋的事情。

person match {
  case person: Manager = {
    person match {
      case person: Investor =>
        // do something, the person is both a manager and investor
      case _ =>
        // do something, the person is only a manager
    }
  case person: Investor =>
    // do something, the person is only an investor.
  case _ =>
    // person is neither, error out.
}

这简直太丑了。有什么建议吗?

I'm using the Scala match/case statement to match an interface of a given java class. I want to be able to check if a class implements a combination of interfaces. The only way I can seem to get this to work is to use nested match/case statements which seems ugly.

Lets say I have a PersonImpl object which implements Person, Manager and Investor. I want to see if PersonImpl implements both Manager and Investor. I should be able to do the following:

person match {
  case person: (Manager, Investor) =>
    // do something, the person is both a manager and an investor
  case person: Manager =>
    // do something, the person is only a manager
  case person: Investor =>
    // do something, the person is only an investor
  case _ =>
    // person is neither, error out.
}

The case person: (Manager, Investor) just doesn't work. In order to get it to work I have to do the following which seem ugly.

person match {
  case person: Manager = {
    person match {
      case person: Investor =>
        // do something, the person is both a manager and investor
      case _ =>
        // do something, the person is only a manager
    }
  case person: Investor =>
    // do something, the person is only an investor.
  case _ =>
    // person is neither, error out.
}

This is just plain ugly. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-11-12 02:21:56

试试这个:

case person: Manager with Investor => // ...

with 用于您可能想要表达类型交集的其他场景,例如在类型绑定中:

def processGenericManagerInvestor[T <: Manager with Investor](person: T): T  = // ...

顺便说一句 - 不是推荐的做法,但是— 您也可以像这样测试它:if (person.isInstanceOf[Manager] && person.isInstanceOf[Investor]) ...


编辑:这对我来说很有效:

trait A
trait B
class C

def printInfo(a: Any) = println(a match {
  case _: A with B => "A with B"
  case _: A => "A"
  case _: B => "B"
  case _ => "unknown"
})

def main(args: Array[String]) {
  printInfo(new C)               // prints unknown
  printInfo(new C with A)        // prints A
  printInfo(new C with B)        // prints B
  printInfo(new C with A with B) // prints A with B
  printInfo(new C with B with A) // prints A with B
}

Try this:

case person: Manager with Investor => // ...

with is used for other scenarios where you might want to express type intersection, e.g. in a type bound:

def processGenericManagerInvestor[T <: Manager with Investor](person: T): T  = // ...

By the way — not that this is recommended practice, but — you can always test it like this as well: if (person.isInstanceOf[Manager] && person.isInstanceOf[Investor]) ....


Edit: this works well for me:

trait A
trait B
class C

def printInfo(a: Any) = println(a match {
  case _: A with B => "A with B"
  case _: A => "A"
  case _: B => "B"
  case _ => "unknown"
})

def main(args: Array[String]) {
  printInfo(new C)               // prints unknown
  printInfo(new C with A)        // prints A
  printInfo(new C with B)        // prints B
  printInfo(new C with A with B) // prints A with B
  printInfo(new C with B with A) // prints A with B
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文