我遇到了一个令人费解的案例类类型推断问题。这是一个最小的例子:
trait T[X]
case class Thing[A, B, X](a: A, f: A => B) extends T[X]
def hmm[X](t: T[X]) = t match {
case Thing(a, f) => f("this really shouldn't typecheck")
}
Scala 决定
a: Any
和 f: Any => Any
,但这是不合适的;他们真的应该有类型 a: SomeTypeA
和 f: SomeTypeA => SomeTypeB
,其中 SomeTypeA
和 SomeTypeB
是未知类型。另一种说法是我认为假设
Thing.unapply
方法应该看起来像def unapply[X](t: T[X]): Option[(A, A => B)] forSome { type A; type B } = {
t match {
case thing: Thing[_, _, X] => Some((thing.a, thing.f))
}
}
此版本在
f("this really shouldn't typecheck")
处正确地给出了类型错误.这看起来像是编译器中的错误,还是我遗漏了什么?
编辑:这是在 Scala 2.10.3 上。
最佳答案
Mark Harrah 在 Freenode 的 #scala channel 中指出了这一点:是的,这是一个错误。
https://issues.scala-lang.org/browse/SI-6680
关于Scala 模式匹配推断 `Any` 而不是存在类型,是否会破坏类型安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20359696/