You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fsharp/style-guide/formatting.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,6 +245,17 @@ SomeClass.Invoke ()
245
245
String.Format (x.IngredientName, x.Quantity)
246
246
```
247
247
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
+
248
259
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:
249
260
250
261
```fsharp
@@ -1152,6 +1163,37 @@ match l with
1152
1163
| [] -> failwith "Couldn't find David"
1153
1164
```
1154
1165
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
+
1155
1197
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`/`|`.
0 commit comments