这似乎是一个简单的事情,但我无法理解它......
这编译:
object CanFoo1 {
def foo(): Unit = {
println("Yup, I can foo alright")
}
}
object CanFoo2 {
def foo(): Unit = {
println("And I can foo with the best")
}
}
trait A {
type CanFoo = { def foo(): Unit }
def fooers: Seq[CanFoo]
}
class B extends A {
def fooers = Seq(
// CanFoo1, // <- won't compile when this is uncommented
CanFoo2
)
}
但取消注释该// CanFoo1,
行给出:
error: type mismatch;
found : Seq[Object]
required: Seq[B.this.CanFoo]
(which expands to) Seq[AnyRef{def foo(): Unit}]
def fooers = Seq(
^
one error found
所以看起来编译器理解一个只包含一个元素Seq(CanFoo2)
(或Seq(CanFoo1)
)的集合是正确的类型,但是当两个对象都在集合中时它放弃了吗?我在这里做错了什么?