Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 15, 2025

Problem

The existing documentation for the F# nameof pattern didn't clearly demonstrate its usefulness. The example showed:

let f (str: string) =
    match str with
    | nameof str -> "It's 'str'!"
    | _ -> "It is not 'str'!"

This appeared to work identically to using a string literal "str", making it unclear why developers should use nameof instead of plain strings.

Solution

Updated both pattern-matching.md and nameof.md to include:

  1. A practical, real-world example showing event deserialization with discriminated unions:
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
  1. Clear explanation of benefits:

    • Automatic updates when refactoring (if you rename OrderCreated to OrderPlaced, the pattern automatically updates)
    • Compiler validation prevents typos by ensuring the symbol exists
    • Code remains consistent during refactoring
  2. Kept the original simple example for completeness, but moved it after the more compelling use case

The new examples make it immediately clear why nameof pattern is better than hardcoded string literals, especially in scenarios like message deserialization, event handling, and API routing where string values represent type or case names.

Testing

  • ✅ All markdown linting passes (markdownlint-cli2)
  • ✅ F# code examples compile and execute successfully
  • ✅ Changes follow repository documentation guidelines
Original prompt

This section details on the original issue you should resolve

<issue_title>usefulness of the nameof pattern is not clear</issue_title>
<issue_description>A compelling use for the nameof pattern is not presented. I can’t see the usefulness from the example given.

let f (str: string) =
    match str with
    | nameof str -> "It's 'str'!"
    | _ -> "It is not 'str'!"

seems to work identically to this:

let f2 (s: string) =
    match s with
    | "str" -> "It's 'str'!"
    | _ -> "It is not 'str'!"

The page linked does not help to clarify.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@BillWagner tagging @vzarytovskii for recommendation on the best fix.

Fixes #36396


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


Internal previews

📄 File 🔗 Preview link
docs/fsharp/language-reference/nameof.md docs/fsharp/language-reference/nameof
docs/fsharp/language-reference/pattern-matching.md docs/fsharp/language-reference/pattern-matching

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Oct 15, 2025
@Copilot Copilot AI changed the title [WIP] Clarify usefulness of the nameof pattern in documentation Improve F# nameof pattern documentation with practical examples Oct 15, 2025
@Copilot Copilot AI requested a review from BillWagner October 15, 2025 12:53
Copilot finished work on behalf of BillWagner October 15, 2025 12:53
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review October 15, 2025 20:56
@BillWagner BillWagner requested review from a team as code owners October 15, 2025 20:56
@BillWagner BillWagner requested review from IEvangelist and Copilot and removed request for a team October 15, 2025 20:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses feedback about the F# nameof pattern documentation by adding practical, real-world examples that clearly demonstrate its usefulness over string literals. The changes improve developer understanding by showing compelling use cases for the nameof pattern in event handling and message deserialization scenarios.

  • Added a practical event handling example using discriminated unions that demonstrates the refactoring safety benefits of nameof patterns
  • Explained the specific advantages of using nameof over string literals (automatic updates during refactoring, compiler validation, consistency)
  • Added AI usage disclosure frontmatter to both documentation files

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
docs/fsharp/language-reference/pattern-matching.md Enhanced nameof pattern section with practical event handling example and clear benefit explanations
docs/fsharp/language-reference/nameof.md Updated nameof pattern matching section with the same practical example and benefits

@BillWagner BillWagner requested a review from a team October 15, 2025 20:56
@BillWagner BillWagner merged commit 86a6a1d into main Oct 16, 2025
13 checks passed
@BillWagner BillWagner deleted the copilot/clarify-usefulness-of-nameof-pattern branch October 16, 2025 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dotnet-fsharp/svc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

usefulness of the nameof pattern is not clear

3 participants