我遇到了案例类令人费解的类型推理问题.这是一个最小的例子:
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")
}
Run Code Online (Sandbox Code Playgroud)
斯卡拉决定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))
}
}
Run Code Online (Sandbox Code Playgroud)
此版本正确地给出了类型错误f("this really shouldn't typecheck").
这看起来像编译器中的错误,还是我错过了什么?
编辑:这是在Scala 2.10.3上.
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |