From a3f384a712e744c9d459209c550b78523582c680 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:20:50 +0000 Subject: [PATCH 1/4] Initial plan From fec773709141edb438953f99fc9869ccfe2fdfac Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:27:03 +0000 Subject: [PATCH 2/4] Add documentation for wildcard pattern's ability to match nothing Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../language-reference/pattern-matching.md | 12 ++++++++- .../snippets/fsharp/lang-ref-2/snippet4818.fs | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/fsharp/lang-ref-2/snippet4818.fs diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 4ac2d0e7c6a87..6df3eb5c5d4c1 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -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 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..8dab464e0cad8 --- /dev/null +++ b/samples/snippets/fsharp/lang-ref-2/snippet4818.fs @@ -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) \ No newline at end of file From eac92dbc58f19de056af5b0229a959af524aaf93 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 06:46:00 +0000 Subject: [PATCH 3/4] Remove unit pattern from wildcard pattern documentation Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com> --- docs/fsharp/language-reference/pattern-matching.md | 1 - samples/snippets/fsharp/lang-ref-2/snippet4818.fs | 13 +++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 6df3eb5c5d4c1..4f83e232995e1 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -177,7 +177,6 @@ The wildcard pattern is represented by the underscore (`_`) character and matche 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)] diff --git a/samples/snippets/fsharp/lang-ref-2/snippet4818.fs b/samples/snippets/fsharp/lang-ref-2/snippet4818.fs index 8dab464e0cad8..f5092e160eafa 100644 --- a/samples/snippets/fsharp/lang-ref-2/snippet4818.fs +++ b/samples/snippets/fsharp/lang-ref-2/snippet4818.fs @@ -1,25 +1,18 @@ // 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 +// Example 1: Wildcard ignoring function parameters let ignoreAllParams _ _ = "Ignores all input" -// Example 3: Wildcard in destructuring, ignoring elements +// Example 2: Wildcard in destructuring, ignoring elements let getFirstOnly (first, _) = first -// Example 4: Using wildcard to ignore optional values +// Example 3: 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) \ No newline at end of file From 6000d05705a2a6acff66e67c43817162c4f86f9e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 07:01:26 +0000 Subject: [PATCH 4/4] Remove misleading sentence about wildcard matching nothing Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com> --- docs/fsharp/language-reference/pattern-matching.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 4f83e232995e1..b9d23162ffe36 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -172,17 +172,12 @@ 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 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. -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 destructuring data structures, it ignores specific elements that are not needed. +The following code shows some additional uses of the wildcard pattern: [!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 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.