1
爲什麼下面的代碼不能編譯?帶有f有界多態的斯卡拉存在奇怪的編譯錯誤
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]
然後012編譯。 - 所以這表明
A[_<:SomeB]
對於r3
不夠具體。但爲什麼不呢? - 爲什麼
A[_<:SomeB]
不夠val r5: (A[_<:SomeB]) = (r3)
但不是r9
?
嗯,這似乎有點相關:http://stackoverflow.com/questions/28674486/existential-types-for-f-bounded-polymorphic-types-and-non-generic-subtypes – jhegedus
看起來像另一個bug。你的第一個代碼片段可以用dotty/master編譯。 – OlivierBlanvillain
嗯....奇怪...我應該開始使用dotty? :) – jhegedus