Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# style - prefer () or <|

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()
like image 916
Torbjörn Gyllebring Avatar asked Dec 07 '25 04:12

Torbjörn Gyllebring


2 Answers

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.

like image 179
Brian Avatar answered Dec 09 '25 00:12

Brian


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".

like image 28
namin Avatar answered Dec 09 '25 00:12

namin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!