Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion docs/fsharp/language-reference/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,17 @@ The record pattern is used to decompose records to extract the values of fields.

## Wildcard Pattern

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.
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.

In addition to matching any input, the wildcard pattern can effectively match "nothing" in certain contexts:

- When used in function parameters, it ignores the arguments entirely, treating them as if they were not there.
- When matching the unit type `()`, which represents the absence of a meaningful value.
- When destructuring data structures, it ignores specific elements that are not needed.

[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4818.fs)]

The wildcard pattern is demonstrated in many code examples in this topic. See the preceding code for one example.

## Patterns That Have Type Annotations

Expand Down
25 changes: 25 additions & 0 deletions samples/snippets/fsharp/lang-ref-2/snippet4818.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Wildcard pattern matching "nothing" examples
open System

// Example 1: Wildcard matching unit type (representing nothing)
let processUnit x =
match x with
| () -> "Matched unit (nothing)"

// Example 2: Wildcard ignoring function parameters
let ignoreAllParams _ _ = "Ignores all input"

// Example 3: Wildcard in destructuring, ignoring elements
let getFirstOnly (first, _) = first

// Example 4: Using wildcard to ignore optional values
let handleEmpty opt =
match opt with
| Some _ -> "Has something"
| None -> "Has nothing"

// Usage
printfn "%s" (processUnit ())
printfn "%s" (ignoreAllParams 42 "test")
printfn "%d" (getFirstOnly (1, "ignored"))
printfn "%s" (handleEmpty None)
Loading