2

Scala 的重载方法解析在这里看起来很奇怪:

abstract class Parent { type T }
class Child extends Parent { type T = Int }

def f[T <: Parent](a: Array[T]): Array[T#T] = null
def f[T](a: T): Array[T] = null

val s = f(Array(new Child))                   // OK; calls the first f
val t: Array[Child#T] = f(Array(new Child))   // type mismatch; tries to apply the second f

尽管 s 与 t 的类型相同,但使用第一个 f 成功评估了 s,但仅给出类型提示会使编译器尝试第二个 f 并失败,需要 Int 作为其参数。我最终避免使用方法重载,但只是出于好奇,有人可以告诉我这里有什么问题吗?

4

0 回答 0