Variant types can take invariant positions in object-protected (protected[this]) abstract type, but these abstract types can also be exposed publicly [sometimes*]. For example, both of the following are legal, although they shouldn't be:
// 1. Type bound with +T in invariant position, then exposed publicly:abstractclassInvariantBoundAbstractType[+T] {
protected[this] typeTSeq<:MutableList[T]
varv:TSeq
}
// 2. Locally, the type is bound properly, but not for subclass:abstractclassVariantBoundAbstractType[+T] {
protected[this] typeTSeq<:Seq[T]
protected[this] valv:TSeqdefget():TSeq= v
// None of this is immediately erroneous,// but can be if subclassed as below:
}
classInvariantConcreteType[+T](protected[this] valv:MutableList[T]) extendsVariantBoundAbstractType[T] {
protected[this] typeTSeq=MutableList[T] // no complaints for this assignment
}
The violations are classic:
// 1.valwrapper= (newInvariantBoundAbstractType[Int] {
typeTSeq=MutableList[Int]
varv=MutableList(42)
})
(wrapper: InvariantBoundAbstractType[Any]).v +="string!"
wrapper.v.last +42// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer//2.varA=MutableList(42)
(newInvariantConcreteType[Int](A):VariantBoundAbstractType[Any]).get() +="string!"A.last +42// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
*The typechecker catches this hole occasionally when the violation is simpler. For example, the following fails correctly:
classDirectInvariantConcreteType[+T] {
protected[this] typeTSeq=MutableList[T] // assignment makes set() a no-go, whereas lower-bounding would be finedefset(v: TSeq):Unit// "covariant type T occurs in invariant position"
}
The text was updated successfully, but these errors were encountered:
acrylic-origami
changed the title
Type holes in protected[this] abstract type constructors
Type holes in protected[this] abstract types
Apr 8, 2017
Variant types can take invariant positions in object-protected (
protected[this]) abstract type, but these abstract types can also be exposed publicly [sometimes*]. For example, both of the following are legal, although they shouldn't be:The violations are classic:
*The typechecker catches this hole occasionally when the violation is simpler. For example, the following fails correctly:
The text was updated successfully, but these errors were encountered: