Skip to content

Lesson 5 of 9 · 26m

Functors and Sharing Constraints Without Fear

Treat functors as compile-time dependency injection, understand when they help, and avoid turning simple architecture into type-level bureaucracy.

Functors have a reputation problem. The name sounds abstract, the error messages can be unkind, and beginners often meet them through library documentation before anyone explains the practical point. Here it is: a functor is a module parameterized by another module. It is compile-time dependency injection with type checking.

Start with the dependency, not the syntax

Suppose you need a cache that works with different key types. The key module provides a type and comparison:

module type ORDERED = sig
  type t
  val compare : t -> t -> int
end

A functor consumes that capability:

module Make_cache (Key : ORDERED) = struct
  type 'a t = (Key.t * 'a) list
  let empty = []
  let find key entries =
    List.find_map (fun (k, v) ->
      if Key.compare key k = 0 then Some v else None) entries
end

The cache implementation does not know whether keys are user IDs, symbols, or tokens. It knows exactly one capability: comparison.

Instantiating a functor is explicit wiring

module String_cache = Make_cache (struct
  type t = string
  let compare = String.compare
end)

This is the architectural benefit: wiring is visible, checked, and substitution happens before runtime. There is no service locator guessing which implementation appeared at midnight.

You will use functors most often through Map.Make and Set.Make; the resulting map keys carry the ordering module's type, which prevents accidentally mixing keys ordered under different notions of identity.

Sharing constraints connect abstract types

Sometimes two modules independently expose abstract types, and you must explain that they are the same type. That is what sharing constraints do:

module Make_router (S : STORE) (K : ORDERED with type t = S.key) = struct
  let route key = S.encode key
end

Without the sharing constraint, the compiler correctly treats S.key and K.t as potentially different abstract types. The annotation is not noise; it is type equality documentation.

A functor is justified when the dependency genuinely varies and the variation matters at compile time: ordering, equality, monadic interface, backend implementation, numeric representation. It is usually overkill for application business logic that only has one implementation. Records of functions or plain module dependencies can be simpler.

Field notes

  • Functor errors often mean a signature mismatch, missing type equality, or hidden representation — read the required signature first.
  • struct and sig nesting is powerful; deeply nested modules quickly become hard to navigate.
  • Applicative versus generative functor behavior matters for type identity. You rarely need to exploit it in application code, but library authors do.
  • If every application module is a functor, architecture has become ideology. Use the simplest mechanism that makes the dependency honest.

Was this lesson helpful?

Sign in to rate