-
Notifications
You must be signed in to change notification settings - Fork 90
Explicit syntax for irrefutable matches? #213
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
Comments
The problem is that with pattern matching, there’s not nearly as many cases for “binding but not detecting existence” as there is for “only match on existence”. Syntax should privilege the common, and pattern matching-related, case. |
Having read good chunks of this discussion over the couple of issues it's come up in, I think I understand where you're coming from. But... Personally, I disagree that this is a problem. I see destructuring as a short-form of potentially-nested property access and binding. It doesn't do any checking of object structure in the same way ordinary property access doesn't. On the other hand, I see pattern matching as a construct whose primary purpose is checking of object structure. Because of this, my default expectation is that If I understand your proposal correctly, you're effectively arguing for |
You know ... I think I've got to agree with you two. |
This is a continuation of a conversation that started in this issue.
The issue is that there's a discrepancy between destructuring and pattern matching that may confuse developers. When destructuring, any non-existent properties are set to undefined by default (as opposed to throwing an error), while when pattern matching, non-existent properties cause the pattern to fail.
This discrepancy causes issues, such as the following:
Here's one solution, that's similar to what was proposed in the previous discussion (but tweaked a bit). We make the identifiers from syntax such as
{ x }
and[x]
by default create bindings and not do any matching. To do an irrefutable match, you must add a "!" after the identifier.Note that a future proposal could add the "!" into the destructuring syntax as well:
One criticism @hax brought up previously was the fact that we're making the default behavior of
{ x }
less useful. I would argue that there's plenty of use cases for binding potentially non-existent values out there, so it would be good to provide some sort of way to do so, and, I don't think it's that bad to get into the habit of using "!" after your irrefutable matches - it's not a lot of extra baggage. But, this is still an important point to consider and I can certainly see where @hax is coming from.The text was updated successfully, but these errors were encountered: