The OCaml manual has a chapter about this; in short, you’re meeting the value restriction because the 'a
in 'a ref
is an invariant type variable, so it becomes a weak type variable '_a
when trying to “return” a unit -> 'a ref
from failwith
. This doesn’t happen with the 'a
in 'a list
because it’s covariant, which have relaxed restrictions.
This restriction exists to prevent you from using a global instance of 'a ref
as both int ref
and string ref
at the same time, for example. Your function will work if you use failwith
inside it, however:
let create () : 'a t = failwith "..."