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

Named binding patterns name^pattern in pattern matching, function parameters, declarations, for loops; fix complex bindings in for loops #1668

Merged
merged 18 commits into from
Jan 4, 2025

Conversation

edemaine
Copy link
Collaborator

@edemaine edemaine commented Jan 3, 2025

New features:

  • name^pattern or name^ pattern is equivalent to pattern, but also binds the whole value to name.
    • For example, function Comp(props^{a, b}) binds props, a, and b
    • This is like name@pattern in Haskell, and pattern as name in Ocaml and Python. It's motivated by our existing {name^: pattern} syntax.
    • It works in pattern matching, function parameters, and declarations (:=, .=, const, let, var).
    • It works in = assignment, but not yet at the top level. Note a small ambiguity: x^{y} = z previously meant x ^ ({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 of z. But I think it's cleaner and more symmetric to allow it.)
    • It works in 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.
  • In pattern matching, name < 5 now acts like the pattern < 5 but also binds the matching value to name.
    • This is especially useful for matching exceptions. We can now use try ... catch e <? RangeError to both match and get the exception e.

There's a new gatherSubbindings helper that maybe should be part of gatherBindingCode. 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 of splices and thisAssignments 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)

@edemaine edemaine changed the title Named binding patterns name^pattern in pattern matching, function parameters, and declarations Named binding patterns name^pattern in pattern matching, function parameters, declarations, for loops Jan 3, 2025
@edemaine edemaine changed the title Named binding patterns name^pattern in pattern matching, function parameters, declarations, for loops Named binding patterns name^pattern in pattern matching, function parameters, declarations, for loops; fix complex bindings in for loops Jan 3, 2025
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.

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.

@edemaine edemaine merged commit 33bcc40 into main Jan 4, 2025
3 checks passed
@edemaine edemaine deleted the caret branch January 4, 2025 18:36
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.

2 participants