Question

Which of theese two alternatives do you find yourself using most often, and which is more "idiomatic"?

  1. f arg (obj.DoStuff())
  2. f arg <| obj.DoStuff()
Was it helpful?

Solution

Overall, I don't know that one or the other is more idiomatic.

Personally, the only time I use <| is with "raise":

raise <| new FooException("blah")

Apart from that, I always use parens. Note that since most F# code uses curried functions, this does not typically imply any "extra" parens:

f arg (g x y)

It's when you get into non-curried functions and constructors and whatnot that it starts getting less pretty:

f arg (g(x,y))

We will probably at least consider changing the F# languages rules so that high-precedence applications bind even more tightly; right now

f g()

parses like

f g ()

but a lot of people would like it to parse as

f (g())

(the motivating case in the original question). If you have a strong opinion about this, leave a comment on this response.

OTHER TIPS

Because type inference works from left to right, a bonus of using |> is that it allows F# to infer the type of the argument of the function.

As a contrived example,

[1; 2; 3] |> (fun x -> x.Length*2)

works just fine, but

(fun x -> x.Length*2) [1; 2; 3]

complains of "lookup on object of indeterminate type".

I use () much much more often, but thats just preference, I'm pretty sure that <| is more idomatic, but I use () by habit.

Whenever possible, I much prefer |> because it reads from left to right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top