Skip to content

Commit 636adf1

Browse files
authored
Clarify F# pattern matching spacing to align with expression formatting (#48943)
1 parent 806fd67 commit 636adf1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/fsharp/style-guide/formatting.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ SomeClass.Invoke ()
245245
String.Format (x.IngredientName, x.Quantity)
246246
```
247247

248+
These same formatting conventions apply to pattern matching. F# style values consistent formatting:
249+
250+
```fsharp
251+
// ✔️ OK - Consistent formatting for expressions and patterns
252+
let result = Some(value)
253+
254+
match result with
255+
| Some(x) -> x
256+
| None -> 0
257+
```
258+
248259
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:
249260

250261
```fsharp
@@ -1152,6 +1163,37 @@ match l with
11521163
| [] -> failwith "Couldn't find David"
11531164
```
11541165

1166+
Pattern matching formatting should be consistent with expression formatting. Do not add a space before the opening parenthesis of pattern arguments:
1167+
1168+
```fsharp
1169+
// ✔️ OK
1170+
match x with
1171+
| Some(y) -> y
1172+
| None -> 0
1173+
1174+
// ✔️ OK
1175+
match data with
1176+
| Success(value) -> value
1177+
| Error(msg) -> failwith msg
1178+
1179+
// ❌ Not OK, pattern formatting should match expression formatting
1180+
match x with
1181+
| Some (y) -> y
1182+
| None -> 0
1183+
```
1184+
1185+
However, do use spaces between separate curried arguments in patterns, just as in expressions:
1186+
1187+
```fsharp
1188+
// ✔️ OK - space between curried arguments
1189+
match x with
1190+
| Pattern arg (a, b) -> processValues arg a b
1191+
1192+
// ❌ Not OK - missing space between curried arguments
1193+
match x with
1194+
| Pattern arg(a, b) -> processValues arg a b
1195+
```
1196+
11551197
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`/`|`.
11561198

11571199
```fsharp

0 commit comments

Comments
 (0)