Skip to content

Add draft spec for C# pattern-matching changes.#3361

Merged
gafter merged 5 commits into
dotnet:masterfrom
gafter:master-patterns3
Apr 17, 2020
Merged

Add draft spec for C# pattern-matching changes.#3361
gafter merged 5 commits into
dotnet:masterfrom
gafter:master-patterns3

Conversation

@gafter

@gafter gafter commented Apr 15, 2020

Copy link
Copy Markdown
Member

No description provided.

Comment thread proposals/patterns3.md Outdated
Comment thread proposals/patterns3.md Outdated
Comment thread proposals/patterns3.md Outdated
Comment thread proposals/patterns3.md
Comment thread proposals/patterns3.md
Comment thread proposals/patterns3.md Outdated
Comment thread proposals/patterns3.md Outdated
@gafter

gafter commented Apr 16, 2020

Copy link
Copy Markdown
Member Author

I think I've addressed all of the review comments.

Comment thread proposals/patterns3.md
This does not work today because, for an *is-pattern-expression*, the pattern variables are considered *definitely assigned* only where the *is-pattern-expression* is true ("definitely assigned when true").

Supporting this would be simpler (from the programmer's perspective) than also adding support for a negated-condition `if` statement. Even if we add such support, programmers would wonder why the above snippet does not work. On the other hand, the same scenario in a `switch` makes less sense, as there is no corresponding point in the program where *definitely assigned when false* would be meaningful. Would we permit this in an *is-pattern-expression* but not in other contexts where patterns are permitted? That seems irregular.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

note (from our talk today): i think we should support this. users will be able to say any of:

if (e is int)
if (e is not int)
if (e is int i)
// but not
if (e is not int i)

Despite that they can write:

if (!(e is int)) -> if (e is not int)
// but not
if (!(e is int i)) -> if (e is not int i)

It's a very strange inconsistency.

@gafter gafter Apr 17, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could do that but ONLY (1) for the is-pattern expression (no other places patterns could be used), and only (2) if the not is at the top level only. For other cases, there isn't any clear place that the pattern variables would be definitely assigned. If we did this, people would find these restrictions to be strange.

I'm OK considering this relaxation as a separate proposal to be considered later. Specifying it will be a bit convoluted: currently, pattern variables are only definitely assigned when the pattern matches.

In any case, the intent here is to check in a document describing what we already accepted and implemented for C# 9 (not that it cannot change).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed with Cyrus. It may make the spec convoluted, but I think the regularity would be worth it. We should definitely put this in the open issues tracker to make sure we don't forget about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is on the LDM agenda.

@333fred 333fred left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Make sure to have a tracker for the is not int i part.

@gafter
gafter merged commit 6901635 into dotnet:master Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants