3 ms·
> Thus it is clear that a stable of white horses is not a stable of horses. You're not making sense any more.
by toolslive 6y 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 6y 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 6y agoYes! it's about (co|contra)-variance and mutability, and your type system should understand this.