Skip to content

Commit 86a6a1d

Browse files
CopilotBillWagner
andauthored
Improve F# nameof pattern documentation with practical examples (#49258)
* Initial plan * Improve nameof pattern documentation with clearer examples Co-authored-by: BillWagner <[email protected]> * Fix markdown linting issues - add blank lines around lists Co-authored-by: BillWagner <[email protected]> * Add ai-usage frontmatter to indicate AI-assisted modifications Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent f98a1d6 commit 86a6a1d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

docs/fsharp/language-reference/nameof.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Nameof
33
description: Learn about the nameof operator, a metaprogramming feature that allows you to produce the name of any symbol in your source code.
44
ms.date: 11/12/2020
5+
ai-usage: ai-assisted
56
---
67

78
# Nameof
@@ -77,7 +78,33 @@ The reason why the syntax is different is to align with other F# intrinsic opera
7778

7879
## Nameof in pattern matching
7980

80-
The [`nameof` pattern](pattern-matching.md#nameof-pattern) lets you use `nameof` in a pattern match expression like so:
81+
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.
82+
83+
A practical example is deserializing events or messages where string values represent type or case names:
84+
85+
```fsharp
86+
type EventType =
87+
| OrderCreated
88+
| OrderShipped
89+
| OrderDelivered
90+
91+
let handleEvent eventName data =
92+
match eventName with
93+
| nameof OrderCreated -> printfn "Processing order creation: %s" data
94+
| nameof OrderShipped -> printfn "Processing order shipment: %s" data
95+
| nameof OrderDelivered -> printfn "Processing order delivery: %s" data
96+
| _ -> printfn "Unknown event type: %s" eventName
97+
98+
handleEvent "OrderCreated" "Order #123" // matches first case
99+
```
100+
101+
Using `nameof` instead of string literals like `"OrderCreated"` provides several benefits:
102+
103+
- If you rename a discriminated union case, the pattern automatically updates.
104+
- The compiler prevents typos by ensuring the symbol exists.
105+
- Your code remains consistent during refactoring.
106+
107+
You can also use `nameof` with parameters:
81108

82109
```fsharp
83110
let f (str: string) =

docs/fsharp/language-reference/pattern-matching.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Pattern Matching
33
description: Learn how patterns are used in F# to compare data with logical structures, decompose data into constituent parts, or extract information from data.
44
ms.date: 11/12/2020
5+
ai-usage: ai-assisted
56
---
67
# Pattern Matching
78

@@ -238,7 +239,34 @@ let let str = // str is inferred to be `string | null`
238239

239240
## Nameof pattern
240241

241-
The `nameof` pattern matches against a string when its value is equal to the expression that follows the `nameof` keyword. for example:
242+
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.
243+
244+
A common use case is deserializing data where string values represent type or case names:
245+
246+
```fsharp
247+
type EventType =
248+
| OrderCreated
249+
| OrderShipped
250+
| OrderDelivered
251+
252+
let handleEvent eventName data =
253+
match eventName with
254+
| nameof OrderCreated -> printfn "Processing order creation: %s" data
255+
| nameof OrderShipped -> printfn "Processing order shipment: %s" data
256+
| nameof OrderDelivered -> printfn "Processing order delivery: %s" data
257+
| _ -> printfn "Unknown event type: %s" eventName
258+
259+
handleEvent "OrderCreated" "Order #123" // matches first case
260+
handleEvent "OrderShipped" "Order #123" // matches second case
261+
```
262+
263+
This approach is better than using string literals (like `"OrderCreated"`) because:
264+
265+
- If you rename `OrderCreated` to `OrderPlaced`, the pattern automatically updates.
266+
- The compiler ensures that the symbol exists, preventing typos.
267+
- Your code remains consistent when refactoring.
268+
269+
You can also use `nameof` with parameters:
242270

243271
```fsharp
244272
let f (str: string) =

0 commit comments

Comments
 (0)