tc39 / proposal-pattern-matching

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

Keep matching operators and matching related control-structures (i.e. guard clauses) in separate syntax space #254

Closed theScottyJam closed 2 years ago

theScottyJam commented 2 years ago

The README presents the following example, that showcases how to use the with "operator":

when (/(\d+) \* (\d+)/) with ([_, left, right]): process(left, right);

Other examples use with more like this instead:

when (/(\d+) \* (\d+)/ with [_, left, right]): process(left, right);

Without the clutter, this is the difference:

// syntax 1
when (...) with (...): ...
// vs syntax 2
when (... with ...): ...

Is this intentional? The "syntax 2" option makes sense to me, because you're supposed to be able to use with on any pattern including nested ones. The "syntax 1" option does not make sense to me, it sort of puts this "with" operator into the same syntax-space as a guard-clause, and I don't really like that. If this is intentional, are there also plans on making it so you can use "and" and "or" in a top-level way (the syntax 1 option) as well?

theScottyJam commented 2 years ago

To put it another way, this makes about as much sense to me as allowing syntax like this:

if (x + y) === (3) {
  ...
}
mpcsh commented 2 years ago

I don't think that was intentional - I believe your syntax 2 is our intention. For more clarity, see here (and the rest of that thread)

Jack-Works commented 2 years ago

Hello, this is a bug in the readme. Already fixed in #251, but not merged yet.

theScottyJam commented 2 years ago

Ok, sounds good. Thanks for clarifying. 👍️