Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion docs/fsharp/language-reference/nameof.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand Down
30 changes: 29 additions & 1 deletion docs/fsharp/language-reference/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) =
Expand Down