(Introduced in OCaml 5.4)
Since OCaml 5.4, array literal syntax [| e1 ; e2 ; ... ; eN |] can be used to denote values of type floatarray as well as 'a array, both in expression and pattern positions. This syntax is also used to display floatarray values in the toplevel.
The compiler matches the expected type of the expression or pattern with the type of the literal, in a manner analogous to the disambiguation of constructors and record fields (see 1.4.1).
In the absence of an expected type, array literals are assumed to be of type 'a array.
In the following examples, the array literals are assigned type floatarray:
The same disambiguation mechanism is used for array literals appearing in patterns:
In the example below, x is assigned type float array:
However, the following does not work:
Here the information learned from the use of a as a floatarray cannot be propagated back to the array literal. In general, expected type information cannot be propagated “backwards”.
In the following example, type disambiguation works because the type information learned in the then branch of the conditional is propagated to the else branch, which occurs “later”.
Such cases trigger warning 18 not-principal, if enabled.