trait B {
type MyInnerType
def foo: MyInnerType
}
object B1 extends B {
type MyInnerType = Double
val foo = 3.0
}
trait A {
type MyInnerType
val b: B
def foo(x: b.MyInnerType): MyInnerType
def bar(y: MyInnerType): Unit
}
object A1 extends A {
type MyInnerType = Int
val b = B1
def foo(x: b.MyInnerType) = 1
def bar(y: MyInnerType) {}
}
object A2 extends A {
type MyInnerType = String
val b = B1
def foo(x: b.MyInnerType) = "a"
def bar(y: MyInnerType) {}
}
val as = Seq(A1, A2)
as foreach { a => a.bar(a.foo(a.b.foo)) } // wrong, a.foo(a.b.foo) infers to Any
但是,如果a.foo
不带参数,则一切正常并a.foo
推断为a.MyInnerType
. 如果我施放它也可以.asInstanceOf[a.MyInnerType]
。有什么解释吗?