[throughout, I say “Caml”, not “Ocaml” because this is an inheritance from Caml and the designers of Caml, and I want to pay due respect to them.]
A long, long, long time ago, on a continent far, far away, I asked Pierre Weis why Caml(-Heavy, and by extension, Caml-Light) didn’t allow this. He patiently explained that this … “weakness” was a direct consequence of the Caml design principle of ALWAYS having principal type schemes. To wit, if a Caml program (in the core language, sure, not objects, modules, extensible variants, etc) fails to type-check, then adding type-coercions WILL NOT improve the situation.
This is a really, really important point (in Caml’s favor).
the point one important selling point of ML typically is that you don’t have to write down types – the compiler infers them for you. The “principal type schemes” property guarantees that the compiler will always come up with a type that is better than the type you would have written (i.e., that your type can be derived from the type the compiler infers) UNLESS your program is ill-typed. And all of this, without type-annotations/coercions. A language with this property really delivers on the promise of “type inference” (comma maaaan).
SML was not such a language. When you write the expression
in the presence of multiple record-types (say, “t1”, “t2”) that all enjoy a “field1” member, the compiler cannot infer a type for the above expression, but it can infer a type for the expression
(x : t1).field1
and (of course)
(x : t2).field1
I for one found that (again, long long ago on a continent far far away and ever since) the tradeoff was well-worth it. It lets you write code faster, because you never, ever, ever worry that your type-check failure is due to inadequate type-casting – b/c if it were, then you have to figure out which of the umpteen gazillion places where you didn’t decorate something with a type … needs a type-coercion.
That latter bit is … well, all I can say is, that’s nuts, that is.
P.S. I feel I must (again) note that this was all due to Pierre Weis’ patient and generous explanation. None of this (except for the experience report) is from me. Erm, well, except (of course) for any errors in my explanation.