Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion docs/fsharp/style-guide/formatting.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
Expand Down