With the article - https://blog.codecentric.de/en/2015/04/the-scala-type-system-parameterized-types-and-variances-part-2/
-
Workaround to use a contravariant type in a return type:
abstract class Box[-A] { def foo[B <: A](): B } is allowed.
I can fix with the code
class BoxB[-F](f: F) {
def fruit[U <: F]: U = f.asInstanceOf[U]
}
But don’t understand very well the reason (I can get the sense of A <: B so M[B] <: M[A] but can’t bridge the missing gap that I am not aware of). Are there any more concrete examples that can help me understand this? I found I have a hard time understanding as it’s a bit too abstract to me. But with the example (for List[+A]), for instance, originally we have a list of Banana List(Banana, Banana), so it’s a list of Banana List[Banana]. Now the list adds another kind of fruit, say, Apple. In order to describe the list, it’s more appropriate to describe the list with List[Fruit] because the list is no longer just a list of Banana.
Thanks