Another key reason for this, is separate compilation. Each file of OCaml source code is a unit of compilation, and they can all be compiled and linked separately.
Suppose someone gives you a compiled bytecode implementation file foo.cmo
and you need to link it to your program. The compiled bytecode is a black box. The compiler doesn’t know anything about its implementation ie what is the ordering of the fields of the record type foo
. Hence you also need to provide an interface file foo.mli
to be able to link your program with the bytecode file. And so, the order of the record fields declared in foo.mli
needs to match the actual order of the fields used internally in foo.cmo
.