为什么在使用管道的 Scala 模式中将变量绑定到异常类型被推断为类型 "Throwable"?

Why binding a variable to Exception types in a Scala pattern with pipes is inferred to type "Throwable"?

我在 Scala 中有以下代码摘录:

case e@(_: java.text.ParseException | _: NumberFormatException | _: ArrayIndexOutOfBoundsException) =>

为表达式 e 推断的最具体类型是 Throwable 而不是 Exception。为什么会这样?

我真的不能确定,因为你没有粘贴完整的代码,但我相信你得到的 Throwable 不是来自模式匹配,而是来自你正在匹配的东西对自己;这就是我重现问题的方法:

scala> trait Foo
defined trait Foo

scala> trait Fox extends Foo
defined trait Fox

scala> trait Bar extends Fox
defined trait Bar

scala> trait Baz extends Fox
defined trait Baz

scala> def test(x: Foo) = x match { case e @ (_: Bar | _: Baz) => e }
test: (x: Foo)Foo

我认为问题在于,当您在模式匹配中使用 | 时,您无法按照自己的意愿使用推理,因此您只是取回了您提供的类型在输入中。

这是一个错误,即SI-8881