Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern mat docs f/b #24041

Closed
DickBaker opened this issue May 4, 2021 · 1 comment · Fixed by #23464
Closed

Pattern mat docs f/b #24041

DickBaker opened this issue May 4, 2021 · 1 comment · Fixed by #23464
Assignees
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc fundamentals/subsvc Pri2

Comments

@DickBaker
Copy link
Contributor

  1. I HATE those examples with gash trailing "," as it looks like you forgot something, especially bonkers after the final catchall "_"
  2. the Type tests could simply be "(heldReference as IDisposable)?.Dispose();" without if{} and extra variable
  3. relational patterns doesn't have explicit "_" catchall [needs more brain cycles to validate all cases). I suggest
    string WaterState(int tempInFahrenheit) =>
    tempInFahrenheit switch
    {
    < 32 => "solid",
    32 => "solid/liquid transition",
    212 => "liquid / gas transition",
    > 212 => "gas",
    _ => "liquid"
    };
  4. Multiple inputs: WTF is that "var 0 => 0," when "_ => 0" is simpler [remember KISS!]
  5. anyway imho all the double results should be 0d etc
  6. FWIW given the pattern hierarchy [for equal precedence] you might order most likely first for perf

Document Details

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

@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label May 4, 2021
@BillWagner BillWagner added the doc-enhancement Improve the current content [org][type][category] label May 5, 2021
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label May 5, 2021
@BillWagner
Copy link
Member

You make some valid points @DickBaker I'll address each of them below:

  1. The trailing , is a mild recommendation for patterns. It makes rearranging switch arms easier (per your point 6). This should stay as is.
  2. That's valid. Do you have a better canonical example that is easy to understand quickly? My other thought was using IEnumerable<T> and Count(), replacing with IList<T> and Count for perf reasons. That might be a better example.
  3. Yes, but that's by design. Note this sentence: "The final two switch arms handle the cases for the melting point and the boiling point. Without those two arms, the compiler warns you that your logic doesn't cover every possible input."
  4. Yes, but without that example, there's no example of the var pattern. (Admittedly the var pattern is used for almost the same scenarios as the discard pattern, so both code forms are valid.)
  5. There's also a school of thought that decimal is a better choice in modern CPUs.
  6. That's a micro-optimization that is worth the cost for explaining more about how the pattern matching features work.

I've added the "up for grabs" label for possible updates on (2) and (4) if someone has better example ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org][type][category] dotnet-csharp/svc fundamentals/subsvc Pri2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants