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 name^: value binds name, while name: value never does (except for identifiers) #1663

Merged
merged 2 commits into from
Dec 27, 2024

Conversation

edemaine
Copy link
Collaborator

@edemaine edemaine commented Dec 27, 2024

Fixes #1660 by no longer binding name in object pattern properties with literal values: name: "string", name: 42, or name: /regex/. The general rule is now this: if a property has a value (other than an identifier), then it doesn't get bound. This seems like a natural extension of how destructuring assignment works.

New feature name^: value binds name. This also works when checking other properties. For example, the pattern {parent^: {type: "BlockStatement", expressions}} binds parent and expression in two destructurings: {parent} = x, {expressions} = parent.

BREAKING CHANGE: Pattern matching with {name: literal} no longer binds name; use {name^: literal} to bind.

Copy link
Contributor

@STRd6 STRd6 left a comment

Choose a reason for hiding this comment

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

Looks good! It seems natural to extend to regular destructuring at some point as well.

@edemaine
Copy link
Collaborator Author

Oh, that's a fun idea, like {x^: [y]} := z. (It already works within if.)

I wonder if there's a natural way to do it at the top level too, not just for object properties. Like I want function Comp(props) to bind props but also destructure... Something like function Comp(props^: {name}). This is related to the desire to have as in a pattern, like [a, b] as arr. I'm not sure we want to call it as because it's not a type operation. So maybe something involving ^.

@edemaine edemaine merged commit 246bab8 into main Dec 27, 2024
3 of 4 checks passed
@edemaine edemaine deleted the pattern-bind branch December 27, 2024 17:16
@edemaine edemaine changed the title Pattern name^: value binds name, while name: value never does Pattern name^: value binds name, while name: value never does (except for identifiers) Jan 3, 2025
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.

Pattern match creates unused variable
2 participants