Skip to content

Instantly share code, notes, and snippets.

@abreslav
Created March 20, 2014 17:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abreslav/9669252 to your computer and use it in GitHub Desktop.
Save abreslav/9669252 to your computer and use it in GitHub Desktop.
// 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>
}
@abreslav
Copy link
Author

I think it is important to mention that term "polymorphism" may have slightly different meaning depending on context. I criticize subtyping polymorphism, not the parametric polymorphism of course.

What you say is far too theoretical, I'm afraid.

Only parametric polymorphism is Haskell minus irrelevant piculiarities (laziness, immutable data etc). And that's too hard for people to grasp. And to be practical this kind of language would need type classes, that are even harder, and then structural subtyping for record that is impossible to implement efficiently over JVM.

I'm closing the case here. Too many keystrokes. If you want to discuss further, let's talk over skype (andrey.breslav).

@Eliah-Lakhin
Copy link

@llogiq I agree with your point. Actually even the current implementation of Java is covering 95% of all practical use cases.

@Eliah-Lakhin
Copy link

Ok. Andrey, thank you very much for participation and for your time. It was very interesting and informative discussion. I hope we will have a chance to continue it later.

I'm following your project with interest. Albeit we have different points of view in some controversial problems, I must admit JetBrains making the best products in its market. And there is no doubt, Kotlin is one of them.

P.S. I'm going to add you on my Skype contact list, so we will be in touch.

@abreslav
Copy link
Author

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment