tc39 / proposal-pattern-matching

Pattern matching syntax for ECMAScript
https://tc39.es/proposal-pattern-matching/
MIT License
5.46k stars 89 forks source link

Draft spec of syntax #251

Closed Jack-Works closed 2 years ago

Jack-Works commented 2 years ago

Only contains the syntax. Hope Early Errors and RS can be deferred to future PRs.

image

image

image

image

Note

Jack-Works commented 2 years ago

Maybe I should remove this part from this PR? And... any review or approval?

image

ljharb commented 2 years ago

Why would we remove that part?

Jack-Works commented 2 years ago

Why would we remove that part?

The syntax I quoted is not in the pattern matching, but in the destructing, which means it allows you to write the following code:

const ${ Some } with Item = result
ljharb commented 2 years ago

oh, yeah that's definitely wrong :-)

(i'm not the best person to review syntax/grammar; so all i can do is ask about the implications)

Jack-Works commented 2 years ago

I added it just for the symmetry (cause PM has that). Anyone I'll wait for more comments (to remove it or keep it).

ljharb commented 2 years ago

Pattern matching needs to mimic destructuring, but the reverse is very much not true. Patterns shouldn't work in destructuring, and ${} only works inside a template literal or inside a pattern context.

Jack-Works commented 2 years ago

Another backport is .... Should we bring const [a, ...] = b back to the deconstructing? ( cc @hax that have a proposal about iterators )

ljharb commented 2 years ago

No, there's no reason to add it, because destructuring doesn't check the length.

Jack-Works commented 2 years ago

I have removed the backport part.

Jack-Works commented 2 years ago

😂 since there is no review for months, I guess I can merge this first. If there is any problem, we can always open a new PR to fix it. This also allows future specifications of EE and RS.