diff --git a/docs/fsharp/language-reference/nameof.md b/docs/fsharp/language-reference/nameof.md index 575e58125af60..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 @@ -77,7 +78,33 @@ 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..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 @@ -238,7 +239,34 @@ 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) =