In OCaml, referring to constructors defined in other modules can be somewhat awkward. Suppose we have a module like the following.

  1. module Example = struct
  2. type t = Foo | Bar | Baz
  3. end

To write a function that pattern matches on values of type Example.t we could directly refer to the variants as follows.

  1. let f e =
  2. match e with
  3. | Example.Foo -> ...
  4. | Example.Bar -> ...
  5. | Example.Baz -> ...

That is pretty verbose. We could alleviate the problem by opening Example.

  1. open Example
  2. let f e = match e with
  3. | Foo -> ...
  4. | Bar -> ...
  5. | Baz -> ...

That is nicer to look at, but the open potentially brings a lot of things into scope (and not just for f, but for the rest of the file). Using open is generally bad style because it makes it hard for a reader to connect definitions with uses. The open would be less problematic if we could reduce its scope. We can do that by using a local module.

  1. let f e =
  2. let module M = struct
  3. open Example
  4. let res =
  5. match e with
  6. | Foo -> ...
  7. | Bar -> ...
  8. | Baz -> ...
  9. end in
  10. M.res
  11.  

That's pretty verbose too. The approach we've settled on at Jane Street is to use let module to rebind the module to a short name, thereby making the code concise and avoiding the open entirely.

  1. let f e =
  2. let module E = Example in
  3. match e with
  4. | E.Foo -> ...
  5. | E.Bar -> ...
  6. | E.Baz -> ...