3 ms·
But a stable of horses, can accept yellow or black horses. A stable of white horses, cannot accept yellow or black horses. If what one can accept in both cases
by zozbot234 7y ago
But a stable of horses, can accept yellow or black horses. A stable of white horses, cannot accept yellow or black horses. If what one can accept in both cases was the same, it is evident that "white horse" would not differ from "horse". But acceptable and unacceptable horses are mutually contrary. Thus it is clear that a stable of white horses is not a stable of horses.
- toolslive 7y ago> Thus it is clear that a stable of white horses is not a stable of horses. You're not making sense any more.
- nybble41 7y agoA stable that can only contain white horses is not a stable that can contain any kind of horse. Whether you can ignore that distinction depends on whether the stable is in a covariant or contravariant position, i.e. whether you want to get horses from the stable or put horses into the stable. These would be fine: Horse[] genericStable = …; WhiteHorse[] whiteHorseStable = …; genericStable.insert(new WhiteHorse()); Horse someHorse = whiteHorseStable.first(); However, these can fail due to type errors: whiteHorseStable.insert(new BrownHorse()); WhiteHorse horse = genericStable.first(); Because the ability to substitute a subtype for a supertype (or vice-versa) depends on whether the use is covariant or contravariant (or invariant) it becomes difficult to have proper subtype or supertype relationships in object-oriented languages where this distinction is not factored in to the type system. Simplifying a bit, to apply the LSP the language needs to distinguish between getters and setters at the type level and can't treat all methods equally.
- toolslive 7y agoYes! it's about (co|contra)-variance and mutability, and your type system should understand this.