gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:41:24 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com