-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
declaration-site variance annotations of generic type arguments #81
Comments
@Mouvedia Thx, I added Flow. |
https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-function-parameters-bivariant Could you provide a JS example or a link to a ressource? |
We handle polymorphism with the ability to use multiple signatures. Do we need variance annotations as well? Can you show some example usage and compare with multiple signature annotation to more clearly point out advantages or disadvantages? What do variant annotations give us that we don't already have? |
@Mouvedia This is the TypeScript example (of your link) that causes a runtime error: function trainDog(d: Dog) { ... }
function cloneAnimal(source: Animal, done: (result: Animal) => void): void { ... }
let c = new Cat();
// Runtime error here occurs because we end up invoking 'trainDog' with a 'Cat'
cloneAnimal(c, trainDog); As the author of the example states:
We shouldn't do the same fault as TypeScript. There are better ways. For example, this is the equivalent Scala code (run online): abstract class Animal()
case class Dog() extends Animal
case class Cat() extends Animal
object Variance extends App {
def cloneAnimal(source: Animal, done: (Animal) => Unit): Unit = {
done(source)
}
def trainDog(dog: Dog): Unit = {}
val cat = Cat()
// causes compile error:
cloneAnimal(cat, trainDog)
} which results in a compile error
since Scala defines function parameters as contravariant: |
Ill give you a template for your straw-man proposal because for pure javascript developers it's really hard to grasp.
|
Declaration-site variance annotations in other languages
C#, Kotlin, Ceylon:
out
covariancein
contravarianceScala, OCaml, Flow:
+
covariance-
contravarianceWhat should we choose? (see discussion about generic types in #55 and #80)
The text was updated successfully, but these errors were encountered: