-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposal: Extended property patterns #4394
Comments
My 2c: if I saw Given that |
Not exactly. The compiler actually consider Take
Patterns never emit code that might throw an NRE. It's possible that a Deconstruct method or a property getter throw in a pattern match, but that's user code. So I'd say intuitively you should expect that it doesn't, because otherwise it is in fact unexpected. |
I simply stated what the compiler does.
Eh? You can have whatever expectations you like, but I'm stating my expectations. Different people come at these things from different angles, and I'm giving my intuition here. You would do well to at least consider viewpoints other than your own. |
I'll champion this |
I would go this route. We have precedence for this sort of attach as well. An initializer is just a list of expressions. So |
Certainly. I didn't mean to say otherwise and I didn't mean you personally. I apologies if that wasn't clear in my comment. |
I strongly support this proposal. This will help simplify and clarify a lot of patterns that check nested values today. |
Fwiw, I agree with @alrz. Patterns themselves should not throw. The pattern is a declarative way to express what shape the value needs to have. So if you say |
If breaking the public API is an option I'd prefer that one too (that might include IOp as well). We just need to filter out invalid expressions which doesn't seem to be too expensive. |
I would say let's not drive language design based on the public API of Roslyn :). |
Does that mean the language design can or cannot dictate breaking changes? I mean, as far as the language concerns, the grammar is straightforward. The only remaining issue is that how it fits into the current implementation and I just wanted to call it out. |
If we need to make a breaking change in the roslyn API to reflect the reality of the language, then that's what we need to do. We don't want to let the language be hamstrung by the implementation details of the public API of the compiler. |
Note: we would not need to make any breaking changes here. We'd just take one of our standard paths to update an existing langauge grammar rule when it comes to modeling it in teh syntax model. We're well versed on ways to accomplish that. |
To not break the existing API, we could add a simple "compatibility property" which checks if the Expression is an assignment expression ... |
@bernd5 this was already triaged into the 10.0 working set. |
Merged the feature into roslyn's |
Extended property patterns
Summary
Allow property subpatterns to reference nested members.
Motivation
As discussed in #4114 when you want to match a child property, nesting another recursive pattern adds too much noise which will hurt readability with no real advantage.
Detailed design
Semantics
A pattern of the form
{ Property1.Property2: pattern }
is exactly equivalent to{ Property1: { Property2: pattern } }
.This will include the null check for
Nullable<T>
values as it is the case for the expanded form, so we only see the underlying type's members when we dot off of a property pattern.Repeated member paths are allowed. Under the hood, such member accesses are simplified to be evaluated once.
Syntax
Currently a
SubpatternSyntax
usesNameColonSyntax
which is not able to hold a chain of identifiers.There are several avenues we could take:
ExpressoinSyntax
in place of the nameThis would particularly help with parsing and avoiding lookaheads, since patterns and expressions have a common parsing path, we can start with the pattern and if we get to a colon, we just adjust it as the name and continue to parse the actual pattern.
NameSyntax
in place of the nameThis seems to be the "correct" node to use, but is more expensive to parse. Also. if and when we introduce indexer patterns we probably want to relax property patterns to enable nesting those as well e.g.
{ Property1[0].Property2[1]: pattern }
. In that case we'll need to go with option (1) for forward compatibility.QualifiedNameColonSyntax
in addition toNameColonSyntax
This option doesn't quite help with anything other than keeping the API consistent.
In the first two options, we're able to hold a simple identifier so we might want to consider deprecating
NameColon
API.Alternatives
We could use
{ P1?.P2: p }
syntax to make the implicit null check more apparent. But then we still want to considerP1.P2
in case?.
doesn't apply e.g. for structs. Since patterns shouldn't throw in regular usage and we should emit the null-check anyways, that distinction would seem to be unnecessary.Design meetings
The text was updated successfully, but these errors were encountered: