Contravariance: intuition building from first principles
summary
Builds intuition from first principles by showing exact substitutability rules, changing a type parameter’s variance to make code compile, and demonstrating which direction substitution must go. Works through a concrete Animal hierarchy with Rescue/Clinic typeclasses to show why Rescue is covariant (+) and Clinic contravariant (−), then tests the idea with JsonEncoder/JsonDecoder and real Scala compiler behavior, and points to common real-world occurrences (codecs, Show, function inputs).
AI-written summary · about
chapters (7)
- 0:00Introduction and motivation
- 2:56Prerequisites
- 4:52Covariance and subtyping
- 12:26Understanding contravariance
- 17:04Practical use cases
- 23:46Why contravariance is hard
- 29:17Conclusion and Q&A
related talks
44mType Parameter Power Up Variance Bounds and Inference
05mThe diamond of variance
28mSecrets of the Scala Type System
44mTelling the Truth with Types
48mA gentle introduction to type level programming in Scala
11mThe Secrets of Polymorphic Variants
49mThe Trouble With Types
42mLet the Compiler Help You: How to Make the Most of Scala's Type System
