On this page:
#%call
#%call
|>
?>
8.15.0.12

4.2 Function Calls🔗ℹ

An expression followed by a parenthesized sequence of expressions is parsed as an implicit use of the #%call form, which is normally bound to implement function calls.

expression

fun_expr #%call (arg, ...)

 

repetition

fun_expr #%call (repet_arg, ...)

 

expression

fun_expr #%call (arg, ..., _, arg, ...)

 

arg

 = 

arg_expr

 | 

keyword

 | 

keyword: arg_expr

 | 

keyword: body; ...

 | 

repet , ellipses

 | 

& list_expr

 | 

~& map_expr

 

ellipses

 = 

ellipsis

 | 

ellipses , ellipsis

 

ellipsis

 = 

...

A function call. Each arg_expr alone is a by-position argument, and each keyword: arg_expr combination is a by-keyword argument. Function calls can serve as repetitions, where repet_arg is like arg, but with repetitions in place of expressions.

If the arg sequence contains & list_expr or repet , ellipses, then the elements of the list or repetition are spliced into the call as separate by-position arguments.

If the arg sequence contains ~& map_expr, then all the keys in the immutable map produced by map_expr must be keywords, and they must not overlap with the directly supplied keywords or keywords in maps from other ~& map_expr arguments. The keyword-value pairs are passed into the function as additional keyword arguments.

Parallel to fun, a single keyword as an arg is equivalent to the form keyword: id This kind of double duty of a single keyword is sometimes referred to as “punning.” where id is composed from keyword by taking its string form and lexical context.

The case with an immediate _ group among other args is a special case for a function shorthand, and it takes precedence over parsing the _ as an arg. See _ for more information.

See also use_static.

The #%call form is implicitly used when () is used after another expression in an expression position. See also Implicit Forms.

> List.length([1, 2, 3])

3

> List.length #%call ([1, 2, 3])

3

expression

arg_expr |> immediate_callee

 

expression

arg_expr |> fun_expr

 

~order: pipeline

The |> operator applies its second argument as a function to its first argument. That is, arg_expr |> fun_expr is equivalent to fun_expr(arg_expr), except that arg_expr is evaluated before fun_expr, following the textual order. This form is known as a “pipeline.” Accordingly, |> is the “pipe-forward” operator. The conversion is performed syntactically so that static checking and propagation of static information may apply, but arg_expr and fun_expr are parsed as expressions before the conversion. The |> operator declares weaker precedence than all other operators.

Alternatively, the right-hand side can be an immediate callee, in which case the static information for arg_expr is supplied to it.

A _ function shorthand can be especially useful with |>.

> [1, 2, 3] |> List.length

3

> [3, 1, 2]

    |> List.sort(_)

    |> ([0] ++ _ ++ [100])

    |> to_string.map(_)

["0", "1", "2", "3", "100"]

expression

arg_expr ?> immediate_callee

 

expression

arg_expr ?> fun_expr

 

~order: pipeline

Like |>, but if the result of arg_expr is #false, the result of the ?> expression is #false. Static information is propagated in a way analogous to ?..

> #false ?> List.length

#false