3 ms·
This setImmutable would return a new instance of Array<Animal> which would not be Array<Cat> like the original, and would contain any mix of cats and dogs. So
by nlitened 2y ago
This setImmutable would return a new instance of Array<Animal> which would not be Array<Cat> like the original, and would contain any mix of cats and dogs.
So immutability resolves the covariance problem, I think?
- dwattttt 2y agoI've never quite understood enough about types to know why, but: it's more like the difference between read-only and write-only. One of the variances is safe if you only read, and one is safe if you only write.
- deleted 2y ago[deleted]
- PhilipRoman 2y agoMy main point was that mutability and covariance are two orthogonal concepts that just happen to overlap here due to function signature shape. But it seems that you are right, I cannot think of any expression that would type-check with Array<Animal> but not with Array<Cat>. ChatGPT was very unhelpful in trying to produce a counterexample, since it probably trained on too many entry level stackoverflow posts about this issue.