spragg
12/06/2017, 2:45 AMkevin.cianfarini
12/06/2017, 3:52 AMspragg
12/06/2017, 5:09 AMout
. It felt backwards the first time I used it too. If you are passing in the variable then you need to use out
.Andreas Sinz
12/06/2017, 8:31 AMV
as out
, kotlin complains about Type parameter V is declared as 'out' but occurs in 'in' position in type V
spragg
12/06/2017, 8:36 AMin
val ref: Map<String, Class<out Fragment>> = mapOf()
which is what confused me.kevin.cianfarini
12/06/2017, 6:03 PMV : BaseMVPView
in
do anything to achieve contravariance?Andreas Sinz
12/06/2017, 8:27 PMin
kevin.cianfarini
12/06/2017, 8:28 PMAndreas Sinz
12/06/2017, 8:32 PMT: BaseMVPView
limits the available type arguments. passing an Int
into a method foo(n: Number)
is always possible, because kotlin knows that Int
is a subtype of Number
. in
is useful when casting a class from Comparable<Number>
into Comparable<Int>
out
allows you to cast List<Int>
to List<Number>
kevin.cianfarini
12/06/2017, 8:34 PM<? extends Animal>
<T : BaseMVPView>
spragg
12/06/2017, 8:36 PMkevin.cianfarini
12/06/2017, 8:38 PM<? extends Animal>
effectively not the same in kotlin as <T : Animal>
?Andreas Sinz
12/06/2017, 8:40 PM<T: Animal>
is neither covariance nor contravariance. It just limits the Types that T
can bespragg
12/06/2017, 8:41 PM<? extends Animal>
is the same as <out Animal>'
<? super Animal>
is the same as <in Animal>
Andreas Sinz
12/06/2017, 8:42 PMin
and out
are class-level co-/contra-variance, e.g. in
allows you to cast Compare<Animal>
to Compare<Dog>
. With <T: Animal>
does not allow you to cast a class into Comparable<Animal>
into Comparable<Dog>
kevin.cianfarini
12/06/2017, 8:42 PM<? extends Animal>
both limits the types that can be used and also achieves covariance?Andreas Sinz
12/06/2017, 8:55 PM