From dcfc68391fd9f18dcc93a1814c25fc79e3f9a019 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:41:40 +0000 Subject: [PATCH 1/4] Initial plan From 7dfac6bbce6bb03f131cc271a748f8d7f5997cde Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:46:29 +0000 Subject: [PATCH 2/4] Improve nameof pattern documentation with clearer examples Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- docs/fsharp/language-reference/nameof.md | 27 +++++++++++++++++- .../language-reference/pattern-matching.md | 28 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/fsharp/language-reference/nameof.md b/docs/fsharp/language-reference/nameof.md index 575e58125af60..e5b8b2663d605 100644 --- a/docs/fsharp/language-reference/nameof.md +++ b/docs/fsharp/language-reference/nameof.md @@ -77,7 +77,32 @@ The reason why the syntax is different is to align with other F# intrinsic opera ## Nameof in pattern matching -The [`nameof` pattern](pattern-matching.md#nameof-pattern) lets you use `nameof` in a pattern match expression like so: +The [`nameof` pattern](pattern-matching.md#nameof-pattern) lets you use `nameof` in a pattern match expression. This is particularly useful when matching string values against the names of symbols in your code, providing compile-time safety and automatic updates when you refactor. + +A practical example is deserializing events or messages where string values represent type or case names: + +```fsharp +type EventType = + | OrderCreated + | OrderShipped + | OrderDelivered + +let handleEvent eventName data = + match eventName with + | nameof OrderCreated -> printfn "Processing order creation: %s" data + | nameof OrderShipped -> printfn "Processing order shipment: %s" data + | nameof OrderDelivered -> printfn "Processing order delivery: %s" data + | _ -> printfn "Unknown event type: %s" eventName + +handleEvent "OrderCreated" "Order #123" // matches first case +``` + +Using `nameof` instead of string literals like `"OrderCreated"` provides several benefits: +- If you rename a discriminated union case, the pattern automatically updates. +- The compiler prevents typos by ensuring the symbol exists. +- Your code remains consistent during refactoring. + +You can also use `nameof` with parameters: ```fsharp let f (str: string) = diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 83ccd0bff162d..814fb55a8a4b6 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -238,7 +238,33 @@ let let str = // str is inferred to be `string | null` ## Nameof pattern -The `nameof` pattern matches against a string when its value is equal to the expression that follows the `nameof` keyword. for example: +The `nameof` pattern matches against a string when its value is equal to the expression that follows the `nameof` keyword. This pattern is particularly useful when you need to match string values against the names of types, discriminated union cases, or other symbols in your code. Using `nameof` provides compile-time safety because if you rename a symbol, the pattern will automatically use the new name. + +A common use case is deserializing data where string values represent type or case names: + +```fsharp +type EventType = + | OrderCreated + | OrderShipped + | OrderDelivered + +let handleEvent eventName data = + match eventName with + | nameof OrderCreated -> printfn "Processing order creation: %s" data + | nameof OrderShipped -> printfn "Processing order shipment: %s" data + | nameof OrderDelivered -> printfn "Processing order delivery: %s" data + | _ -> printfn "Unknown event type: %s" eventName + +handleEvent "OrderCreated" "Order #123" // matches first case +handleEvent "OrderShipped" "Order #123" // matches second case +``` + +This approach is better than using string literals (like `"OrderCreated"`) because: +- If you rename `OrderCreated` to `OrderPlaced`, the pattern automatically updates. +- The compiler ensures that the symbol exists, preventing typos. +- Your code remains consistent when refactoring. + +You can also use `nameof` with parameters: ```fsharp let f (str: string) = From 9654b2ccc7ed27d297ddc68ea53ddcae73cd9454 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:48:33 +0000 Subject: [PATCH 3/4] Fix markdown linting issues - add blank lines around lists Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- docs/fsharp/language-reference/nameof.md | 1 + docs/fsharp/language-reference/pattern-matching.md | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/fsharp/language-reference/nameof.md b/docs/fsharp/language-reference/nameof.md index e5b8b2663d605..e986b3a11db7e 100644 --- a/docs/fsharp/language-reference/nameof.md +++ b/docs/fsharp/language-reference/nameof.md @@ -98,6 +98,7 @@ handleEvent "OrderCreated" "Order #123" // matches first case ``` Using `nameof` instead of string literals like `"OrderCreated"` provides several benefits: + - If you rename a discriminated union case, the pattern automatically updates. - The compiler prevents typos by ensuring the symbol exists. - Your code remains consistent during refactoring. diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 814fb55a8a4b6..4e29e389fd32a 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -260,6 +260,7 @@ handleEvent "OrderShipped" "Order #123" // matches second case ``` This approach is better than using string literals (like `"OrderCreated"`) because: + - If you rename `OrderCreated` to `OrderPlaced`, the pattern automatically updates. - The compiler ensures that the symbol exists, preventing typos. - Your code remains consistent when refactoring. From 31344c1fede5bb1f246436da00af4cdef78e3511 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 15 Oct 2025 12:50:24 +0000 Subject: [PATCH 4/4] Add ai-usage frontmatter to indicate AI-assisted modifications Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- docs/fsharp/language-reference/nameof.md | 1 + docs/fsharp/language-reference/pattern-matching.md | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/fsharp/language-reference/nameof.md b/docs/fsharp/language-reference/nameof.md index e986b3a11db7e..b23bffdb00351 100644 --- a/docs/fsharp/language-reference/nameof.md +++ b/docs/fsharp/language-reference/nameof.md @@ -2,6 +2,7 @@ title: Nameof description: Learn about the nameof operator, a metaprogramming feature that allows you to produce the name of any symbol in your source code. ms.date: 11/12/2020 +ai-usage: ai-assisted --- # Nameof diff --git a/docs/fsharp/language-reference/pattern-matching.md b/docs/fsharp/language-reference/pattern-matching.md index 4e29e389fd32a..72cfab1495da0 100644 --- a/docs/fsharp/language-reference/pattern-matching.md +++ b/docs/fsharp/language-reference/pattern-matching.md @@ -2,6 +2,7 @@ title: Pattern Matching description: Learn how patterns are used in F# to compare data with logical structures, decompose data into constituent parts, or extract information from data. ms.date: 11/12/2020 +ai-usage: ai-assisted --- # Pattern Matching