作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了一个令人费解的案例类类型推断问题。这是一个最小的例子:
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")
}
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")
处正确地给出了类型错误.
最佳答案
Mark Harrah 在 Freenode 的 #scala channel 中指出了这一点:是的,这是一个错误。
https://issues.scala-lang.org/browse/SI-6680
关于Scala 模式匹配推断 `Any` 而不是存在类型,是否会破坏类型安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20359696/
我是一名优秀的程序员,十分优秀!