-
Notifications
You must be signed in to change notification settings - Fork 35
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
Named binding patterns name^pattern
in pattern matching, function parameters, declarations, for
loops; fix complex bindings in for
loops
#1668
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
edemaine
changed the title
Named binding patterns
Named binding patterns Jan 3, 2025
name^pattern
in pattern matching, function parameters, and declarationsname^pattern
in pattern matching, function parameters, declarations, for loops
edemaine
changed the title
Named binding patterns
Named binding patterns Jan 3, 2025
name^pattern
in pattern matching, function parameters, declarations, for loopsname^pattern
in pattern matching, function parameters, declarations, for
loops; fix complex bindings in for
loops
STRd6
approved these changes
Jan 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, very nice improvements!
Seeing the examples helps me appreciate the syntax. It seems powerful and natural with room to expand in the future as well.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New features:
name^pattern
orname^ pattern
is equivalent topattern
, but also binds the whole value toname
.function Comp(props^{a, b})
bindsprops
,a
, andb
name@pattern
in Haskell, andpattern as name
in Ocaml and Python. It's motivated by our existing{name^: pattern}
syntax.:=
,.=
,const
,let
,var
).=
assignment,but not yet at the top level. Note a small ambiguity:x^{y} = z
previously meantx ^ ({y} = z)
. I think this is worth overriding, as it's not useful to binary XOR with an object or array. (It's also not particularly critical at the top level, as all it's doing is making a copy ofz
. But I think it's cleaner and more symmetric to allow it.)for ... of/in
loops. These were actually broken with existing@prop
and splices. The following now work and did not before:for @x of y
for [...x, y] of z
{name^: pattern}
is now shorthand for{name: name^pattern}
, and it is now implemented this way.for
loops, not just pattern matching, as suggested in Patternname^: value
bindsname
, whilename: value
never does (except for identifiers) #1663 (comment)name < 5
now acts like the pattern< 5
but also binds the matching value toname
.try ... catch e <? RangeError
to both match and get the exceptione
.There's a new
gatherSubbindings
helper that maybe should be part ofgatherBindingCode
. They're now almost always used together. However, in pattern matching, they're used with somewhat different arguments, so I haven't been able to unify yet. (There's also lots ofsplices
andthisAssignments
code that's mostly duplicated in various places, so I guess this is par for the course.)TODO: Documentation (once the feature/syntax is approved)