views:

320

answers:

4

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()
A: 

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

Rayne
Rayne, I started to grok F# recently. Do you use it for commercial development or just for fun? I'm very interested in real-world F# apps
aku
Just for fun right now, but it's very well suited for real world apps, this language is beautiful. It can be used pretty much to the same extent C# can only more consise elegant, and readable code!
Rayne
Rayne, agree. I really like it. I hope they will get us RTM version soon.
aku
I use it to automate performance tests and write utilities and scripts at work. Hopefully it will gain traction.
Torbjörn Gyllebring
A: 

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

Benjol
+4  A: 

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.

Brian
After reading this and the following threads on FSHubhttp://cs.hubfs.net/forums/thread/3665.aspxhttp://cs.hubfs.net/forums/thread/5421.aspxI'm signing the "make f g() parse like f (g())" petition as soon as I can find a pen! "Fixing" this would make utilizing the framework so much prettier.
Torbjörn Gyllebring
I would also like to see f g() parse like f (g()) to make using method calls from the framework more natural. But, I'm worried it's too late now, considering we already have production release...
Stephen Swensen
It actually is still possible to change; the language differentiates between 'application' (`g x`) and 'high precedence application' (`g(x)` with no space between the `g` and the open-paren), and so `f g(x)` never compiles today, regardless of whether `f` takes 1 or 2 curried arguments (`f g (x)` passes 2 args to `f`, and `f (g(x))` passes one arg to `f`). So it's possible for a future revision of the language to 'admit more legal programs' as a non-breaking change. But it would be awfully subtle (making the whitespace meaningful to differential legal programs) and so is unlikely.
Brian
A: 

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

namin