I am playing around with the typesystem of scala and found a strange case. I have a strong reason to believe, I don't understand covariant and covariance.
This is my problem case:
I have two classes, Point and ColorPoint, which is a subclass of Point.
class Point(val x : Int, val y : Int)
class ColorPoint(x : Int, y : Int, val red : Int, val green : Int, val blue : Int) extends Point(x,y)
This class casts B to A, while B should be a supertype of A:
class CoVariance[+A]{
def cast[B >: A](x : B) : A = {
return x.asInstanceOf[A]
}
}
This class casts B to A, while B should be a supertype of A:
class ContraVariance[-A]{
def cast[B, A <: B](x : B) : A = {
return x.asInstanceOf[A]
}
}
Case 1:
val co = new CoVariance[Point]
val color_point = new ColorPoint(1,2,3,4,5)
val point_co = co.cast(color_point)
println(point_co.x)
If I write this out:
// Covariance[Point] ->
// cast[B :> Point](x : B) : Point -> (fill in ColorPoint)
// Cast[ColorPoint :> Point] : Point
I would expect this to be incorrect, because ColorPoint is not a supertype of Point, but scala doesn't complain.
Next one:
val contra = new ContraVariance[Point]
val color_point_contra = new ColorPoint(1,2,3,4,5)
val point_contra = contra.cast(color_point_contra)
println(point_contra.x)
If I write this out:
// ContraVariance[Point] ->
// cast[B, Point <: B](x : B) : Point -> (fill in ColorPoint)
// cast[ColorPoint, Point <: ColorPoint] : Point
I also expect this to be incorrect, but scala doesn't complain. I would say Point is not a subtype of ColorPoint.
Is my reasoning correct or am I missing something?