I have a class
class Getable [+T] (val data: T)
And I create a method for the class to print the Number
def printNumber (in: Getable[Number]) = { println("It's " + in.data)}
So it works for
get(new Getable(10.0))
But does not work for
val g = new Getable(10.0)
get(g)
I get this error message
scala> get(g)
<console>:16: error: type mismatch;
found : Getable[Double]
required: Getable[Number]
get(g)
^
But overcoming the error message is the reason why we use covariance.
You have to hint a compiler with a type of Getable
.
val g: Getable[Number] = new Getable(10.0)
or
val g = new Getable[Number](10.0)