I have following code to understand covariant and lower bound, I intentionally make the code have compiling errors.
The getOrElse works, which is similar from the getOrElse method of Option[+T].
I would ask why getOrElse2 and getOrElse3 don't work to better understand covariant and lower bound.
The compiling error is pasted as comments in the code:
class MyOption[+A](val x: A) {
def get():A = x
//similar to Option.getOrElse,which works
def getOrElse[B>:A ](default: => B): B = {
if (x != null) x else default
}
//Compiling Error: Expression of type A doesn't conform to Expected type B
def getOrElse2[B, A<:B ](default: => B): B = {
if (x != null) x else default
}
//Covariant type A occurs in controvariant position in type A of value B
def getOrElse3[B <: A](default:=>B): A = {
if (x != null) x else default
}
}