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
42 changes: 42 additions & 0 deletions docs/fsharp/style-guide/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ SomeClass.Invoke ()
String.Format (x.IngredientName, x.Quantity)
```

These same formatting conventions apply to pattern matching. F# style values consistent formatting:

```fsharp
// ✔️ OK - Consistent formatting for expressions and patterns
let result = Some(value)

match result with
| Some(x) -> x
| None -> 0
```

You may need to pass arguments to a function on a new line as a matter of readability or because the list of arguments or the argument names are too long. In that case, indent one level:

```fsharp
Expand Down Expand Up @@ -1152,6 +1163,37 @@ match l with
| [] -> failwith "Couldn't find David"
```

Pattern matching formatting should be consistent with expression formatting. Do not add a space before the opening parenthesis of pattern arguments:

```fsharp
// ✔️ OK
match x with
| Some(y) -> y
| None -> 0

// ✔️ OK
match data with
| Success(value) -> value
| Error(msg) -> failwith msg

// ❌ Not OK, pattern formatting should match expression formatting
match x with
| Some (y) -> y
| None -> 0
```

However, do use spaces between separate curried arguments in patterns, just as in expressions:

```fsharp
// ✔️ OK - space between curried arguments
match x with
| Pattern arg (a, b) -> processValues arg a b

// ❌ Not OK - missing space between curried arguments
match x with
| Pattern arg(a, b) -> processValues arg a b
```

If the expression on the right of the pattern matching arrow is too large, move it to the following line, indented one step from the `match`/`|`.

```fsharp
Expand Down