Chapter 8 Language extensions
This chapter describes language extensions and convenience features that are implemented in OCaml, but not described in the OCaml reference manual.
22 Generalized open statements
(Introduced in 4.08)
|
This extension makes it possible to open any module expression in module structures and expressions. A similar mechanism is also available inside module types, but only for extended module paths (e.g. F(X).G(Y)).
For instance, a module can be constrained when opened with
Another possibility is to immediately open the result of a functor application
Going further, this construction can introduce local components inside a structure,
One important restriction is that types introduced by open struct ... end cannot appear in the signature of the enclosing structure, unless they are defined equal to some non-local type. So:
is OK, but:
is not because x cannot be given any type other than t, which only exists locally. Although the above would be OK if x too was local:
Inside signatures, extended opens are limited to extended module paths,
and not
open struct type t = int end
In those situations, local substitutions(see 8.7.2) can be used instead.
Beware that this extension is not available inside class definitions:
class c = let open Set.Make(Int) in ...