How to achieve `type t = t`?

I’m wondering how to create a type alias for a type of the same name. This problem arises in the following scenario:

type t = A | B
module M = struct
  type t = t (* Error: The type abbreviation t is cyclic *)
end

A solution is to create a t' alias:

type t = A | B
type t' = t
module M = struct
  type t = t' (* works *)
end

Is there a better (clearer) solution which wouldn’t involve creating an extra type alias? I vaguely remember that there might be a clean solution but I couldn’t find it.

1 Like

I found it here by googling the error message.

The solution is:

type t = A | B
module M = struct
  type nonrec t = t
end

(since ocaml 4.02.2)

4 Likes

Prior to OCaml 4.02.2, you could have done instead:

type t = A | B
module M = struct
  type dummy = t
  type t = dummy
end