diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 4ac2d0e7c6a87..b9d23162ffe36 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -174,6 +174,10 @@ The record pattern is used to decompose records to extract the values of fields. 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 following code shows some additional uses of the wildcard pattern: + +[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet4818.fs)] + ## Patterns That Have Type Annotations 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. diff --git a/samples/snippets/fsharp/lang-ref-2/snippet4818.fs b/samples/snippets/fsharp/lang-ref-2/snippet4818.fs new file mode 100644 index 0000000000000..f5092e160eafa --- /dev/null +++ b/samples/snippets/fsharp/lang-ref-2/snippet4818.fs @@ -0,0 +1,18 @@ +// Wildcard pattern matching "nothing" examples + +// Example 1: Wildcard ignoring function parameters +let ignoreAllParams _ _ = "Ignores all input" + +// Example 2: Wildcard in destructuring, ignoring elements +let getFirstOnly (first, _) = first + +// Example 3: Using wildcard to ignore optional values +let handleEmpty opt = + match opt with + | Some _ -> "Has something" + | None -> "Has nothing" + +// Usage +printfn "%s" (ignoreAllParams 42 "test") +printfn "%d" (getFirstOnly (1, "ignored")) +printfn "%s" (handleEmpty None) \ No newline at end of file