4 ms·
Perfect! This is the covariant/contravariant/invariant distinction, right? "Read-only data types (sources) can be covariant; write-only data types (sinks) can
by gugagore 9y ago
Perfect! This is the covariant/contravariant/invariant distinction, right?
"Read-only data types (sources) can be covariant; write-only data types (sinks) can be contravariant. Mutable data types which act as both sources and sinks should be invariant." from https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) https://en.wikipedia.org/wiki/Covariance_and_contravariance_...
- jdmichal 9y agoAbsolutely, and that's another excellent way to explain the problem and come to the same conclusion. The mathematical relationship between rectangles and squares is covariant, because squares have more specific constraints than rectangles. But when you make rectangles mutable, those mutations are contravariant, because they only guarantee to preserve less specific constraints. So you can't make squares from mutable rectangles!