Skip to content

Commit a700eed

Browse files
authored
Add wildcard pattern usage examples to F# documentation (#48741)
1 parent 090c434 commit a700eed

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

docs/fsharp/language-reference/pattern-matching.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ The record pattern is used to decompose records to extract the values of fields.
174174

175175
The wildcard pattern is represented by the underscore (`_`) character and matches any input, just like the variable pattern, except that the input is discarded instead of assigned to a variable. The wildcard pattern is often used within other patterns as a placeholder for values that are not needed in the expression to the right of the `->` symbol. The wildcard pattern is also frequently used at the end of a list of patterns to match any unmatched input. The wildcard pattern is demonstrated in many code examples in this topic. See the preceding code for one example.
176176

177+
The following code shows some additional uses of the wildcard pattern:
178+
179+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4818.fs)]
180+
177181
## Patterns That Have Type Annotations
178182

179183
Patterns can have type annotations. These behave like other type annotations and guide inference like other type annotations. Parentheses are required around type annotations in patterns. The following code shows a pattern that has a type annotation.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Wildcard pattern matching "nothing" examples
2+
3+
// Example 1: Wildcard ignoring function parameters
4+
let ignoreAllParams _ _ = "Ignores all input"
5+
6+
// Example 2: Wildcard in destructuring, ignoring elements
7+
let getFirstOnly (first, _) = first
8+
9+
// Example 3: Using wildcard to ignore optional values
10+
let handleEmpty opt =
11+
match opt with
12+
| Some _ -> "Has something"
13+
| None -> "Has nothing"
14+
15+
// Usage
16+
printfn "%s" (ignoreAllParams 42 "test")
17+
printfn "%d" (getFirstOnly (1, "ignored"))
18+
printfn "%s" (handleEmpty None)

0 commit comments

Comments
 (0)