使用某种结构:
object Foo {
trait Bar[B]
}
trait Foo[A, B, F <: Foo[A, B, F]] {
def baz(fun: A => Foo.Bar[B] => Unit): Unit
}
...为什么存在类型会造成麻烦:
def test[A, F <: Foo[A, _, F]](foo: F) =
foo.baz { a => b => println(b) }
发生以下错误:
<console>:38: error: type mismatch;
found : A => Foo.Bar[(some other)_$1(in type F)] => Unit
forSome { type (some other)_$1(in type F) }
required: A => (Foo.Bar[_$1(in type F)] => Unit)
foo.baz { a => b => println(b) }
^
以下编译:
def test[A, JamesClapperSociopath, F <: Foo[A, JamesClapperSociopath, F]](foo: F) =
foo.baz { a => b => println(b) }
答案 0 :(得分:1)
它必须与存在类型的等价性有关。编译器可能推断b: F#_$1
,然后无法弄清楚两个投影是否相等。
幸运的是,函数在参数类型中是逆变的,因此您只需编写:
def test[A, F <: Foo[A, _, F]](foo: F) =
foo.baz { a => (b: Any) => println(b) }