The blog post by Haoyi about type safety equality in Scala. If you are doing something non-trivial to compute a trivial result, it’s probably an programmer error. Universal equality is just one common case of that.
The Scala language, following its Java heritage, allows you to compare any two values for equality regardless of their respective types. This can be very error prone, since a refactor that changes the type of one of your values may silently result in an equality check that can never return true.
This blog post explores how a “safer” equality check is just one of many features that go against the principles that underlie any type system with subtyping, and achieving it requires a fundamental re-thinking of what it means to be “type safe”.
The article then dives into:
- What are Types and Subtypes?
- The Liskov Substitution Principle
- Strawman: Exact-Type Matches
- Existing violations of Liskov
- Safety beyond Liskov Substitution
- What makes a trivial expression?
- What about Constant Folding?
[Read More]The traditional definition of type-safety, based on types, subtyping and LSP, is deeply ingrained in almost all statically-typed languages. It allows us to prohibit at-compile-time anyone accidentally trying to perform an operation on an expression which might not support it. However, LSP says nothing about another type of issue: that in which a programmer writes code with trivially known results, which is likely to indicate an error or oversight.