最近我偶然发现了一个奇怪的(对我而言)编译器错误消息.请考虑以下代码:
trait Foo {
type Res <: Foo
type Bar[X <: Res]
}
class MyFoo extends Foo {
override type Res = MyFoo
override type Bar[X <: Res] = List[X]
}
type FOO[F <: Foo, R <: Foo, B[_ <: R]] = F { type Res = R;
type Bar[X <: R] = B[X] }
def process[F <: Foo, R <: Foo, B[_ <: R]](f: FOO[F, R, B]) {}
Run Code Online (Sandbox Code Playgroud)
现在,如果我想调用process方法,我必须显式写入类型参数:
process[MyFoo, MyFoo, List](new MyFoo) // fine
Run Code Online (Sandbox Code Playgroud)
如果我写:
process(new MyFoo)
Run Code Online (Sandbox Code Playgroud)
要么
process((new MyFoo): FOO[MyFoo, MyFoo, List])
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
推断的类型参数类型(MyFoo,MyFoo,List [X])不符合预期的类型参数类型(类型F,类型R,类型B).List [X]的类型参数与B类的预期参数不匹配:class List有一个类型参数,但类型B有一个
为什么编译器不能推断出类型(虽然我在调用参数中明确说明了它们)?这class List has one type parameter, but type B has one意味着什么?有东西有一个,但另一个也有一个,这就是为什么它们不适合在一起???
如果我们查看 Scala 编译器,源代码可以帮助我们理解问题所在。我从未对 Scala 编译器做出过贡献,但我发现源代码非常可读,并且我已经对此进行了调查。
scala.tools.nsctypechecker.Infer您只需在 Scala 编译器源中查找错误的一部分即可找到负责类型推断的类。你会发现以下片段:
/** error if arguments not within bounds. */
def checkBounds(pos: Position, pre: Type, owner: Symbol,
tparams: List[Symbol], targs: List[Type], prefix: String) = {
//@M validate variances & bounds of targs wrt variances & bounds of tparams
//@M TODO: better place to check this?
//@M TODO: errors for getters & setters are reported separately
val kindErrors = checkKindBounds(tparams, targs, pre, owner)
if(!kindErrors.isEmpty) {
error(pos,
prefix + "kinds of the type arguments " + targs.mkString("(", ",", ")") +
" do not conform to the expected kinds of the type parameters "+ tparams.mkString("(", ",", ")") + tparams.head.locationString+ "." +
kindErrors.toList.mkString("\n", ", ", ""))
}
Run Code Online (Sandbox Code Playgroud)
所以现在的重点是理解为什么checkKindBounds(tparams, targs, pre, owner)会返回这些错误。如果你沿着方法调用链往下看,你会看到 checkKindBounds 调用另一个方法
val errors = checkKindBounds0(tparams, targs, pre, owner, true)
Run Code Online (Sandbox Code Playgroud)
您会看到问题与检查更高种类类型的边界有关,位于 checkKindBoundsHK 内的第 5784 行:
if (!sameLength(hkargs, hkparams)) {
if (arg == AnyClass || arg == NothingClass) (Nil, Nil, Nil) // Any and Nothing are kind-overloaded
else {error = true; (List((arg, param)), Nil, Nil) } // shortcut: always set error, whether explainTypesOrNot
}
Run Code Online (Sandbox Code Playgroud)
测试未通过,在我的调试器中似乎:
hkargs$1 = {scala.collection.immutable.Nil$@2541}"List()"
arg$1 = {scala.tools.nsc.symtab.Symbols$ClassSymbol@2689}"class List"
param$1 = {scala.tools.nsc.symtab.Symbols$TypeSymbol@2557}"type B"
paramowner$1 = {scala.tools.nsc.symtab.Symbols$MethodSymbol@2692}"method process"
underHKParams$1 = {scala.collection.immutable.$colon$colon@2688}"List(type R)"
withHKArgs$1 = {scala.collection.immutable.Nil$@2541}"List()"
exceptionResult12 = null
hkparams$1 = {scala.collection.immutable.$colon$colon@2688}"List(type R)"
Run Code Online (Sandbox Code Playgroud)
因此,似乎有一个更高种类的参数,类型为 R,但没有提供该参数的值。
如果您实际上返回到 checkKindBounds,您会在代码片段之后看到:
val (arityMismatches, varianceMismatches, stricterBounds) = (
// NOTE: *not* targ.typeSymbol, which normalizes
checkKindBoundsHK(tparamsHO, targ.typeSymbolDirect, tparam, tparam.owner, tparam.typeParams, tparamsHO)
)
Run Code Online (Sandbox Code Playgroud)
其中arityMismatches包含一个元组List,B。现在你也可以看到错误信息是错误的:
类型参数 (MyFoo,MyFoo,List[X]) 的推断类型不符合类型参数的预期类型(类型 F、类型 R、类型 B)。List[X] 的类型参数与类型 B 的预期参数不匹配:类 List 有一个类型参数,但类型 B 有0 个类型参数
事实上,如果您在下面的调用中在第 5859 行放置一个断点
checkKindBoundsHK(tparamsHO, targ.typeSymbolDirect, tparam, tparam.owner, tparam.typeParams, tparamsHO)
Run Code Online (Sandbox Code Playgroud)
你可以看到
tparam = {scala.tools.nsc.symtab.Symbols$TypeSymbol@2472}"type B"
targ = {scala.tools.nsc.symtab.Types$UniqueTypeRef@2473}"List[X]"
Run Code Online (Sandbox Code Playgroud)
结论:
由于某种原因,在处理像您这样的复杂的高级类型时,Scala 编译器推理是有限的。我不知道它来自哪里,也许你想向编译器团队发送一个错误
| 归档时间: |
|
| 查看次数: |
413 次 |
| 最近记录: |