How to get a module type from an interface?

In some cases, you should use

include module type of struct include M end   (* I call it OCaml keyword mantra *)

rather than

include module type of M

since the latter drops the equalities of data types with their originals defined in M.

The difference can be observed by ocamlc -i xxx.mli:

include module type of struct include Complex end

has the following type definition:

type t = Complex.t = { re : float; im : float; }

which means t is an alias of the original Complex.t.

On the other hand,

include module type of Complex

has

type t = { re : float; im : float; }

Without the relation with Complex.t, it becomes a different type from Complex.t: you cannot mix code using the original module and your extended version without the include hack. This is not what you want usually.


You can look at RWO : if you want to include the type of a module (like List.mli) in another mli file :

include (module type of List)

Tags:

Module

Ocaml