diff --git a/docs/fsharp/style-guide/formatting.md b/docs/fsharp/style-guide/formatting.md index 04bcaf5b23aa9..7407af646e9d8 100644 --- a/docs/fsharp/style-guide/formatting.md +++ b/docs/fsharp/style-guide/formatting.md @@ -1,7 +1,7 @@ --- title: F# code formatting guidelines description: Learn guidelines for formatting F# code. -ms.date: 05/14/2018 +ms.date: 11/26/2018 --- # F# code formatting guidelines @@ -25,6 +25,63 @@ When indentation is required, you must use spaces, not tabs. At least one space That said, indentation of programs is a subjective matter. Variations are OK, but the first rule you should follow is *consistency of indentation*. Choose a generally accepted style of indentation and use it systematically throughout your codebase. +## Formatting white space + +F# is white space sensitive. Although most semantics from white space are covered by proper indentation, there are some other things to consider. + +### Formatting operators in arithmetic expressions + +Always use white space around binary arithmetic expressions: + +```fsharp +let subtractThenAdd x = x - 1 + 3 +``` + +Unary `-` operators should always have the value they are negating immediately follow: + +```fsharp +// OK +let negate x = -x + +// Bad +let negateBad x = - x +``` + +Adding a white-space character after the `-` operator can lead to confusion for others. + +In summary, it's important to always: + +* Surround binary operators with white space +* Never have trailing white space after a unary operator + +The binary arithmetic operator guideline is especially important. Failing to surround a binary `-` operator, when combined with certain formatting choices, could lead to interpreting it as a unary `-`. + +### Surround a custom operator definition with white space + +Always use white space to surround an operator definition: + +```fsharp +// OK +let ( !> ) x f = f x + +// Bad +let (!>) x f = f x +``` + +For any custom operator that starts with `*`, you'll need to add a white space to the beginning of the definition to avoid a compiler ambiguity. Because of this, it's recommended that you simply surround the definitions of all operators with a single white-space character. + +### Surround function parameter arrows with white space + +When defining the signature of a function, use white space around the `->` symbol: + +```fsharp +// OK +type MyFun = int -> int -> string + +// Bad +type MyFunBad = int->int->string +``` + ## Formatting blank lines * Separate top-level function and class definitions with two blank lines.