Filip Petrovic (1) [Avatar] Offline
#1
The section uses List interface as the example for covariance and shows only a couple of its methods (get and subList), both having the T in the 'out' positions. However, we know that List interface has methods with T appearing in the 'in' positions, too: some of its own (e.g. indexOf and lastIndexOf), and some inherited (e.g. contains)

Although the methods in question do not change neither the List object nor passed in T object, their signatures contradict with the claims in this section, such is one on page 264, that the type parameter marked with keyword 'out'
T can be used only in "out" position


Perhaps the intention (and a valid one) was to keep examples simple and not go into much detail. However, since the real life example was used, and List is probably the most frequently used interface, I think a few words should be said about its 'dark' side smilie. Perhaps the choice of List is a fortunate one exactly for this reason: here we have an opportunity for learning about the intention to enable covariance on one hand and (the trade-offs and proglems with) implementing it on the other.

Kotlin's designers clearly thought variance is important and that it could be improved from Java, so they introduced 'in' and 'out' variance annotations. But, once one new to Kotlin sees the List interface in its entirety, he/she may wonder if the concept is well thought out and if it works in real life.

Thanks,
Filip