Choosing between f x
, x |> f
and f <| x
is mainly a question of
style. There's no absolute rule for choosing one instead of the
other. The |>
operator is very popular, and it's a good idea to use it.
<|
is less frequent, but if you look in the compiler's sources, you'll find a couple of
uses. For example:
raise <| System.InvalidOperationException (SR.GetString(SR.QillFormedAppOrLet))
if info.precision then
failwithf "%s" <| FSComp.SR.forFormatDoesntSupportPrecision(ch.ToString())
<|
is used to remove a parenthesis, and I think it make the code
more readable when used carefully. When you see it, you know the
following expression is the argument to your function. You don't
have to search for the closing parenthesis. I suggest you use it sparingly, and you should generally avoid mixing <|
and |>
in the same expression, as it can be very
confusing.
I sometimes enjoy using this operator to create a "block", with a
fun
or lazy
keyword.
let f (l: Lazy<_>) = ()
let g (f: _ -> _ -> _) = ()
f <| lazy
let x = 1 + 1
x * x
g <| fun x y ->
let sqr n = n * n
sqr x + sqr y
The block is based on indentation, so it fits quite well in F# code. Thanks
to the <|
operator, you don't need a trailing parenthesis.