The Unix
module defines the Unix_error
exception constructor.
module Unix : sig
exception Unix_error of error * string * string
...
end
Suppose you want to create your own My_unix
module that defines some Unix
utility functions and exports the same Unix_error
. How would you do it?
You can’t redeclare
Unix_error
, since that would make a new constructor, which won’t match
Unix.Unix_error
.
module My_unix = struct
exception Unix_error of error * string * string (* a new exception *)
... my unix functions ...
end
You could include the whole Unix
module, but that pollutes the namespace of
My_unix
unnecessarily.
module My_unix = struct
include Unix
... my unix functions ...
end
A trick to bring just the exception constructor you want into scope is to use a
constrained include of the form include (M : sig ... end)
.
module My_unix = struct
include (Unix : sig exception Unix_error of Unix.error * string * string end)
... my unix functions ...
end
This does require duplicating the exception declaration in the signature, but the type checker will of course guarantee that the declaration you write matches the original, so there is no real chance for error.