|
|
|
|
|
by sparkie
439 days ago
|
|
`String => Cat` and `String => Animal` are the types of the functions themselves - not the arg and return types. If we consider inside the function: foo : arg:String => result:Animal
foo =
;; arg <: String
;; Animal <: result
However, outside of the function, when calling it, the opposite is true. let result = foo (arg)
;; String <: arg
;; result <: Animal
It's easy to get them mixed up when looking at it from the wrong perspective - but we should be looking at functions from the outside, not the inside - so yes, parameters should be contravariant and return types covariant. |
|
Always? In that case, do you know why Dart has a `covariant` keyword [0], which makes function parameters covariant?
[0] https://dart.dev/language/type-system#covariant-keyword