<@U9SNELT4G> your `Box&lt;T&gt;` is invariant, tha...
# getting-started
a
@kiptoo your
Box<T>
is invariant, that means
Box<Int>
is not a subtype of
Box<Number>
https://proandroiddev.com/understanding-generics-and-variance-in-kotlin-714c14564c47
k
hi @Andreas Sinz i now get the general concept (i actually went thru the article you shared earlier, it's excellent). i can tell the compiler that my intention is for IntBox to be a subtype of Box<T> by doing:
open class Box<out T>(t: T) {}
, but what if some of my functions in Box need to take in parameters of type (or subtype) T, e.g
Box.createBox(box: Box<T>)
?
a
@kiptoo you can only have Contravariance or Covariance, meaning
T
only occurs in the in-position or out-position, not both at the same time, otherwise you could create code that fails at runtime
k
good point. looks like i'll have to settle for covariance in this case
thanks!