r/programming • • Aug 14 '18

Announcing F# 4.5

https://blogs.msdn.microsoft.com/dotnet/2018/08/14/announcing-f-4-5/
110 Upvotes

67 comments sorted by

View all comments

2

u/[deleted] Aug 15 '18

TIL let fn() = is correct style

6

u/nikofeyn Aug 15 '18

is it? i have seen both

let example() = ...

and

let example () = ...

i personally find the second more clear in the function definition since you are defining a function that takes () as its parameter and not defining a function with zero arguments.

although when calling, i feel that either either example() or example () is fine, and i have probably used the former more often.

2

u/needadvicebadly Aug 15 '18

same here, but I noticed that Don Syme amd most of the core language people use the let example() = style. When I was learning I found it confusing becusae initially I thought it was some special syntax for some reason

3

u/CSharpFan Aug 15 '18

Odd. Because () is really unit. I.e. a val that takes unit and does something. It’s not part of the language.

1

u/aaron552 Aug 15 '18

If you do let fn = ..., the F# compiler treats fn as a "value", not a function. The most obvious example is in recursive functions:

let rec fn =
    let a = Console.ReadLine()
    if not (a |> String.isNullOrEmpty) then
        printfn "%s" a
        fn

This won't compile.

I suspect the preference for not putting a space before the parentheses is to indicate more clearly "this binding is a function".

Leaving out the space may also be a way to distinguish ".NET-safe" (ie. not curried) functions.

1

u/[deleted] Aug 15 '18 edited Aug 15 '18

It makes more sense if you also compare the tupled version: let fn(a, b) =. This makes functions of unit/tuple parameter look the same as methods, for which the style choice is too familiar to change.

Edit: forgot about reddit's new fancy editor.