Scala 模式匹配推断 `Any` 而不是存在类型,是否会破坏类型安全?

标签 scala types

我遇到了一个令人费解的案例类类型推断问题。这是一个最小的例子:

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: Anyf: Any => Any ,但这是不合适的;他们真的应该有类型 a: SomeTypeAf: SomeTypeA => SomeTypeB ,其中 SomeTypeASomeTypeB是未知类型。

另一种说法是我认为假设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/

相关文章:

scala - 如何基于另一个类似的案例类创建案例类?

string - Scala 中更好的字符串格式

Scala特征和结构类型: can a trait extend a structural type and then call super?

scala - 使用 x.head、x.tail : _* in Spark 的原因

scala - 带有 Gradle 的 Lagom - java.lang.NoClassDefFoundError : scala/Function1$class

python - Mypy 不使用 Type[NamedTuple] 进行类型检查

javascript - 如何使此 typescript 映射功能不那么复杂?

java - java中输入语句的比较

java - 确定数据类型

haskell "Could not deduce", "Non type-variable argument"