为什么下面的代码无法编译?
trait B[T <: B[T]]
case class A[T <: B[T]](t: T)
class C() extends B[C]
val c: C = new C()
val r2: A[_] = A(c) //compiles
val r3: A[_] = A(c) //compiles fine
val r4: A[_] = r3 //compiles fine
val r5: (A[_]) = (r3) //compiles fine
val r6: (A[_], A[_]) = (r3, r3) // does not compile, strange
它给:
Error:(68, 22) type arguments [_$7] do not conform to class A's type parameter bounds [T <: _experiment.akka_persistence.Test2.B[T]]
val r6:(A[_],A[_])=(r3,r3)
编辑:
这是一个相关的、自包含的代码片段:
import scala.language.existentials
trait B[T <: B[T]]
case class A[T <: B[T]](t: T)
class C() extends B[C]
val c: C = new C()
type SomeB = T forSome { type T <: B[T] }
val r3: A[_<:SomeB] = A(c) //compiles fine
val r4: A[C] = A(c) //compiles fine
val r5: (A[_<:SomeB]) = (r3) //compiles fine
val r6:((_<:SomeB),((_<:SomeB))) = (c,c) // compiles fine
val r7:(A[_<:SomeB],((_<:SomeB))) = (A(c),c) // compiles fine
val r8:(A[_<:SomeB],(A[_<:SomeB])) = (A(c),A(c)) // compiles fine
val r10:(A[_<:SomeB],(A[_<:SomeB])) = (A(c),r4) // compiles fine
val r9:(A[_<:SomeB],(A[_<:SomeB])) = (A(c),r3) // does not compile
- 似乎
r4
必须具有类型A[C]
然后r10
编译。 - 所以这表明
A[_<:SomeB]
forr3
不够具体。但为什么不呢? - 还有为什么
A[_<:SomeB]
足够val r5: (A[_<:SomeB]) = (r3)
但不适合r9
?