(Introduced in OCaml 5.4)
Since OCaml 5.4, array literal syntax[|e1;e2; ...;eN|] canbe used to denote values of typefloatarray as well as'a array, both inexpression and pattern positions. This syntax is also used to displayfloatarray values in the toplevel.
The compiler matches the expected type of the expression or pattern with thetype of the literal, in a manner analogous to the disambiguation of constructorsand record fields (see1.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 typefloatarray:
The same disambiguation mechanism is used for array literals appearing inpatterns:
In the example below,x is assigned typefloat array:
However, the following does not work:
Here the information learned from the use ofa as afloatarray cannot bepropagated back to the array literal. In general, expected type informationcannot be propagated “backwards”.
In the following example, type disambiguation works because the type informationlearned in thethen branch of the conditional is propagated to theelsebranch, which occurs “later”.
Such cases trigger warning 18not-principal, if enabled.