3 ms·
The solution to this problem is not to make arrays covariant, but to correctly specify the type signature of MakeJuice: MakeJuice :: Basket<`a> -> void where `
by tomp 15y ago
The solution to this problem is not to make arrays covariant, but to correctly specify the type signature of MakeJuice:
MakeJuice :: Basket<`a> -> void where `a <: Fruit
That is, of course, only if MakeJuice does not modify the array. In general, I think that often the biggest mistake is variance annotation of types. Instead, functions should have (in)variance annotation on every parameter and on the return value. Then, a function that only reads from an array would be covariant in the type of array elements, a function that only writes would be contravariant, and a function that does both would be invariant. No need to artificially limit the usecases of arrays.
- rayiner 15y agoI mean it really depends on what you conceive Basket<Fruit> as meaning in Dart. Essentially, Dart treats it as Basket<T> :: T <: Fruit. If you see it this way, then the obvious question is, how do you write a type declaration for T :: T <: Basket<Fruit>? The answer is: who wants to do that in a optionally typed language?