Skip to content

Commit 7dfac6b

Browse files
CopilotBillWagner
andcommitted
Improve nameof pattern documentation with clearer examples
Co-authored-by: BillWagner <[email protected]>
1 parent dcfc683 commit 7dfac6b

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

docs/fsharp/language-reference/nameof.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,32 @@ The reason why the syntax is different is to align with other F# intrinsic opera
7777

7878
## Nameof in pattern matching
7979

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

82107
```fsharp
83108
let f (str: string) =

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,33 @@ let let str = // str is inferred to be `string | null`
238238

239239
## Nameof pattern
240240

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

243269
```fsharp
244270
let f (str: string) =

0 commit comments

Comments
 (0)