Skip to content

Commit fec7737

Browse files
CopilotBillWagner
andcommitted
Add documentation for wildcard pattern's ability to match nothing
Co-authored-by: BillWagner <[email protected]>
1 parent a3f384a commit fec7737

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,17 @@ The record pattern is used to decompose records to extract the values of fields.
172172

173173
## Wildcard Pattern
174174

175-
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.
175+
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.
176+
177+
In addition to matching any input, the wildcard pattern can effectively match "nothing" in certain contexts:
178+
179+
- When used in function parameters, it ignores the arguments entirely, treating them as if they were not there.
180+
- When matching the unit type `()`, which represents the absence of a meaningful value.
181+
- When destructuring data structures, it ignores specific elements that are not needed.
182+
183+
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4818.fs)]
184+
185+
The wildcard pattern is demonstrated in many code examples in this topic. See the preceding code for one example.
176186

177187
## Patterns That Have Type Annotations
178188

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Wildcard pattern matching "nothing" examples
2+
open System
3+
4+
// Example 1: Wildcard matching unit type (representing nothing)
5+
let processUnit x =
6+
match x with
7+
| () -> "Matched unit (nothing)"
8+
9+
// Example 2: Wildcard ignoring function parameters
10+
let ignoreAllParams _ _ = "Ignores all input"
11+
12+
// Example 3: Wildcard in destructuring, ignoring elements
13+
let getFirstOnly (first, _) = first
14+
15+
// Example 4: Using wildcard to ignore optional values
16+
let handleEmpty opt =
17+
match opt with
18+
| Some _ -> "Has something"
19+
| None -> "Has nothing"
20+
21+
// Usage
22+
printfn "%s" (processUnit ())
23+
printfn "%s" (ignoreAllParams 42 "test")
24+
printfn "%d" (getFirstOnly (1, "ignored"))
25+
printfn "%s" (handleEmpty None)

0 commit comments

Comments
 (0)