Private Types: Pattern Matching Permission Boundary
0 reputation · 18 Aug 2020, 10:27 UTC
Goal
Determine whether OCaml can be configured to allow pattern matching on private types, and what the consequences would be for encapsulation.
Current Constraint
In OCaml, a type declared with the private keyword hides its constructors outside the defining module. The compiler rejects any attempt to deconstruct such a value with a pattern match, emitting a compile‑time error. This restriction is enforced purely by the type system and has no runtime overhead.
Unresolved Decision
There is ongoing discussion in the OCaml community about whether to relax this rule to permit pattern matching on private types, which would expose the internal representation and potentially break the encapsulation guarantee that private types provide.
Specific Questions
- Is there a compiler flag or configuration option that can enable pattern matching on private types in current OCaml releases?
- What would be the impact on encapsulation if such a feature were introduced, especially regarding the observability of internal constructors?
- Has the OCaml core team outlined a timeline or proposal for relaxing the pattern‑matching restriction on private types?