https://kotlinlang.org logo
Title
k

kevin.cianfarini

12/06/2017, 3:52 AM
This is really good, thanks!
so for clarification after reading that article, it looks like V here inherits from BaseMVPView is required?
interface BaseMvpPresenter<in V : BaseMvpView> { fun attachView(view: V) fun detachView() }
s

spragg

12/06/2017, 5:09 AM
-edit- This is wrong. Leaving it for continuity So that example is actually backways. You need to use
out
. It felt backwards the first time I used it too. If you are passing in the variable then you need to use
out
.
-edit- removed because it was wrong.
Regarding the inheritance. V must be either of type BaseMvpView or have extend/inherited it.
a

Andreas Sinz

12/06/2017, 8:31 AM
@spragg @kevin.cianfarini the example isn't working, if I declare
V
as
out
, kotlin complains about
Type parameter V is declared as 'out' but occurs in 'in' position in type V
s

spragg

12/06/2017, 8:36 AM
oh yeah. That is correct. What I said was completely backwards. Sorry about that. It should be
in
I have used it
val ref: Map<String, Class<out Fragment>> = mapOf()
which is what confused me.
Thanks for pointing that out @Andreas Sinz
👍 1
k

kevin.cianfarini

12/06/2017, 6:03 PM
So what im confused about is that in the code I posted above, it seems like covariance is achieved by
V : BaseMVPView
why and how does
in
do anything to achieve contravariance?
and why does it seem like there are two ways to implement covariance
@spragg
a

Andreas Sinz

12/06/2017, 8:27 PM
@kevin.cianfarini first, we are talking about contravariance when using
in
k

kevin.cianfarini

12/06/2017, 8:28 PM
sorry that's what i meant
I was hung up on out from the example in the article
a

Andreas Sinz

12/06/2017, 8:32 PM
and second,
T: 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>
just like
out
allows you to cast
List<Int>
to
List<Number>
k

kevin.cianfarini

12/06/2017, 8:34 PM
so I guess that im asking is in java, covariance is achieved by
<? extends Animal>
in the article
which appears to be possible in kotlin as well
<T : BaseMVPView>
that's where I'm confused
s

spragg

12/06/2017, 8:36 PM
@kevin.cianfarini The diagram at the bottom of the article might help. It contains a table of the relationship of the java version to kotlin.
k

kevin.cianfarini

12/06/2017, 8:38 PM
that doesn't really answer my question. Is
<? extends Animal>
effectively not the same in kotlin as
<T : Animal>
?
a

Andreas Sinz

12/06/2017, 8:40 PM
<T: Animal>
is neither covariance nor contravariance. It just limits the Types that
T
can be
s

spragg

12/06/2017, 8:41 PM
<? extends Animal>
is the same as
<out Animal>'
<? super Animal>
is the same as
<in Animal>
a

Andreas Sinz

12/06/2017, 8:42 PM
in
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>
k

kevin.cianfarini

12/06/2017, 8:42 PM
okay so I guess the missing piece for me is that in Java,
<? extends Animal>
both limits the types that can be used and also achieves covariance?
🙂 1
a

Andreas Sinz

12/06/2017, 8:55 PM
exactly