Type annotation to ensure an extensible's variant given extension is matched exhaustively?

I’d like to strengthen the typing discipline of an extensible variant the way we can do it for polymorphic variants. That is via a type annotation.

I believe this is not possible (and suspect it may never be since extensions may not be unique a given structure). But did I maybe miss something ?

More precisely in the to_string function below I’d like to add an annotation that indicates me that B is unhandled.

type t = ..
module M = struct
  type t += A | B
end

let to_string v = match v with
| M.A -> "A"
| _ -> "Unknown"

This is indeed not currently possible since extensions are not types.
One option that could work would be to extend the #pattern patterns to extensible variants. Then with pattern negation, your use case could be written:

let to_string v = match v with
| M.A -> "A"
| not #M.t -> "Unknown"
1 Like

I googled it, for reference: Pattern negation · Issue #7628 · ocaml/ocaml (github.com)