IT lexicon › Programming › Type class

Type class trait in Rust, typeclass in Haskell

Programming På svenska → Updated: 2026-05-23

A mechanism for ad-hoc polymorphism — define a set of operations a type must support, implement them for different types separately.

Haskell invented the concept (Wadler & Blott, 1989). Rust copied it as "traits". Scala has the "typeclass pattern" via implicits / contextual abstractions. Example: the typeclass Show requires show :: a -> String; every type that implements it can be printed. Different from OO interfaces because (a) implementations can be external (add an instance after the type is defined), (b) static dispatch (zero cost), (c) typeclass methods can return the abstract type (read :: String -> a). A driver of modern language designs.

← Back to the lexicon