以下のタイプチェッカーの厳密さに少し困惑しています — の不変T
位置は、のパラメータリストInv[T]
内でも不変のようです:Variantish
scala> class Inv[T]
defined class Inv
scala> class Variantish[+T, +TVar <: Inv[T]]
<console>:12: error: covariant type T occurs in invariant position in type <: Inv[T] of type TVar
class Variantish[+T, +TVar <: Inv[T]]
^
バリアント型は、通常、不変の引数リストの位置のように見える場所で発生する可能性があります。たとえば、オブジェクトで保護された可視性があります。
class Variantish[+T](protected[this] var v: Inv[T])
そして、以下は同じようにタイプセーフであるようです:
class Variantish[+T, +TVar <: Inv[T]](protected[this] var v: TVar)
上記のチェックはそれほど厳密にする必要がありますか?