Created
March 20, 2014 17:28
-
-
Save abreslav/9669252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Why do we need variance | |
// Invariant class List, just like in Java | |
class List<T> { /* normal list */ } | |
fun printContents(list: List<Any>) { | |
for (item in list) { | |
println(item) | |
} | |
} | |
fun test() { | |
val list: List<String> = listOf("a", "b") | |
printContents(list) // ERROR: List<String> is not a subtype of List<Any> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for the record. On the matter of variance in Java (which is called use-site variance): https://prezi.com/lnw_oiv1gs-j/java-8-vs-kotlin/