-
Notifications
You must be signed in to change notification settings - Fork 107
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 Matching in Pipes #305
Comments
i'm confused, can't you already do this with |
@ljharb You're right about match (%) { ... } covering most of what I proposed. My suggestion was mainly about tighter integration with pipe syntax, potentially offering more concise notation (match%) and implicit % in each case. However, these benefits might not justify a syntax change. What do you think ? |
Having an implicit placeholder is expressly undesirable, and is part of why the pipeline proposal is in its current form. |
Ah I see yes the |
I think it's syntax overload. takeUser(id)
|> (u) => match(u) ... creating a separate syntax for such narrow solutions is the road to syntax hell that C++ follows |
As the last comment says, this is syntax overload. We don't want several ways to achieve one thing (we'd get another Perl, which is horror). If the match proposal gets accepted, this issue will be covered. If that doesn't get accepted, no one will want a syntax that looks like another language but actually works only for pipelines in ES202x. I recommend closing this issue and to follow up on the two proposals separately. Well this one obviously and https://github.com/tc39/proposal-pattern-matching Originally posted by @yordis in #91 (comment) |
Yup, this is indeed a completely separate feature (already being addressed by a different proposal) that doesn't require integration with pipeline at all. I'm gonna go ahead and close this. |
I have a request/proposal to consider. I recognize it would almost certainly be made into its own proposal, but since it's deeply related to
|>
I wanted to raise it here first. The|>
operator provides a clean way to chain operations, but it lacks built-in support for handling different data shapes or cases within the pipeline. I propose extending the pipe operator to support inline pattern matching, which would enhance its expressiveness and flexibility.I propose adding a
match%
keyword that can be used within a pipe chain to perform pattern matching. Here's an example of how it might look:In this syntax,
match%
initiates pattern matching within the pipe, withcase
clauses defining patterns to match against. The right side of=>
in each case can include further piping. This approach allows complex branching logic within pipes without breaking the flow, makes data transformation pipelines more declarative and easier to understand, combines the power of pattern matching with the composability of pipes, and provides a clean way to handle different data shapes or error cases in pipelines.The pattern matching in pipes could also support destructuring, as shown in this example:
It could also integrate well with async operations:
And support guard clauses for more complex conditions:
While this proposal offers significant benefits, there are challenges to consider, including syntax complexity, performance, integration with existing patterns, and tooling support. To move forward, we should gather community feedback, develop a detailed specification, create a basic implementation, and discuss with the TC39 committee.
I welcome thoughts and ideas on this proposal. How do you see this fitting into JavaScript? Any overlooked use cases or challenges?
The text was updated successfully, but these errors were encountered: