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.
14 Extensible variant types
(Introduced in OCaml 4.02)
|
Extensible variant types are variant types which can be extended with new variant constructors. Extensible variant types are defined using ... New variant constructors are added using +=.
Pattern matching on an extensible variant type requires a default case to handle unknown variant constructors:
A preexisting example of an extensible variant type is the built-in exn type used for exceptions. Indeed, exception constructors can be declared using the type extension syntax:
Extensible variant constructors can be rebound to a different name. This allows exporting variants from another module.
Extensible variant constructors can be declared private. As with regular variants, this prevents them from being constructed directly by constructor application while still allowing them to be de-structured in pattern-matching.
14.1 Private extensible variant types
(Introduced in OCaml 4.06)
|
Extensible variant types can be declared private. This prevents new constructors from being declared directly, but allows extension constructors to be referred to in interfaces.