Louis Roché
Louis Roché

Reputation: 896

How to get a module type from an interface?

I would like to have my own implementation of an existing module but to keep a compatible interface with the existing module. I don't have a module type for the existing module, only an interface. So I can't use include Original_module in my interface. Is there a way to get a module type from an interface?

An example could be with the List module from the stdlib. I create a My_list module with exactly the same signature than List. I could copy list.mli to my_list.mli, but it does not seem very nice.

Upvotes: 9

Views: 1332

Answers (2)

camlspotter
camlspotter

Reputation: 9040

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.

Upvotes: 12

Pierre G.
Pierre G.

Reputation: 4441

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)

Upvotes: 3

Related Questions