Typeclasses vs Modules

They do different things

Published on 2025-12-10

Last updated on 2025-12-15 For a long time I’ve noticed that there’s a lot of persistent confusion about the differences and similarities between module systems (as seen in languages like OCaml) and typeclasses (as seen in languages like Haskell or Rust). This is an attempt to clear up the confusion.

The short answer

The main goal of typeclasses as a language construct is to provide ad-hoc polymorphism . This is also known as operator overloading or type-based dispatch . The main point of ad-hoc polymorphism is as a convenience feature for programming in the small: it lets you reuse the same identifiers across types. For example, I might want to use the + symbol to denote both addition on integers and addition on vectors of floats (Importantly, the vector type might be defined in a random library, so we can’t hardcode this behaviour in the language).