Skip to content

Commit

Permalink
more explanations on the labels-omitted status
Browse files Browse the repository at this point in the history
  • Loading branch information
gasche committed Jan 22, 2021
1 parent 3d830ab commit 43a4772
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
31 changes: 31 additions & 0 deletions manual/manual/cmds/comp.etex
Expand Up @@ -358,6 +358,37 @@ command line, and possibly the "-custom" option.

This section describes and explains in detail some warnings:

\subsection{ss:warn6}{Warning 6: Label omitted in function application}

OCaml supports "labels-omitted" full applications: if the function has
a known arity, all the arguments are unlabeled, and their number
matches the number of non-optional parameters, then labels are ignored
and non-optional parameters are matched in their definition
order. Optional arguments are defaulted.

\begin{verbatim}
let f ~x ~y = x + y;;
let test = f 2 3;;
\end{verbatim}

This support for "labels-omitted" application was introduced when
labels were added to OCaml, to ease the progressive introduction of
labels in a codebase. However, it has the downside of weakening the
labeling discipline: if you use labels to prevent callers from
mistakenly reordering two parameters of the same type, labels-omitted
make this mistake possible again.

Warning 6 warns when labels-omitted applications are used, to
discourage their use. When labels were introduced, this warning was
not enabled by default, so users would use labels-omitted
applications, often without noticing.

Over time, it has become idiomatic to enable this warning to avoid
argument-order mistakes. The warning is now on by default, since OCaml
4.13. Labels-omitted applications are not recommended anymore, but
users wishing to preserve this transitory style can disable the
warning explicitly.

\subsection{ss:warn9}{Warning 9: missing fields in a record pattern}

When pattern matching on records, it can be useful to match only few
Expand Down
13 changes: 7 additions & 6 deletions manual/manual/refman/expr.etex
Expand Up @@ -235,17 +235,18 @@ optional and non-optional, will be kept, and the result of the
function will still be a function of these missing parameters to the
body of $f$.

As a special case, if the function has a known arity, all the
arguments are unlabeled, and their number matches the number of
non-optional parameters, then labels are ignored and non-optional
parameters are matched in their definition order. Optional arguments
are defaulted.

In all cases but exact match of order and labels, without optional
parameters, the function type should be known at the application
point. This can be ensured by adding a type constraint. Principality
of the derivation can be checked in the "-principal" mode.

As a special case, OCaml supports "labels-omitted" full applications:
if the function has a known arity, all the arguments are unlabeled,
and their number matches the number of non-optional parameters, then
labels are ignored and non-optional parameters are matched in their
definition order. Optional arguments are defaulted. This omission of
labels may result in a warning, see \ref{ss:warn6}.

\subsubsection*{sss:expr-function-definition}{Function definition}

Two syntactic forms are provided to define functions. The first form
Expand Down

0 comments on commit 43a4772

Please sign in to comment.