Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Subtyping

A <: B reads “A is a subtype of B”: every value of A is acceptable where a B is wanted. The relation is reflexive and transitive, and two bounds frame it for every type — A <: Top and Bot <: A. Top is the greatest type, the position that accepts anything; Bot is the least, the type with no values.

Subtyping has three sources beyond the <: edges a program declares. The numeric tower is one: Nat <: Int <: Real and Nat <: Int <: Decimal hold built in, so an Int flows where a Real is expected. Collections are covariant — List<A> <: List<B> and Option<A> <: Option<B> exactly when A <: B, written [A] and A?. Everything else subtypes by equality alone: tuples, function types, and generic applications match only their own shape.