tc39 / proposal-pattern-matching

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

It would be nice to have this too #334

Closed oleedd closed 2 months ago

oleedd commented 2 months ago

Relational Patterns Currently there are patterns for expressing various types of equality, and kinda an instanceof (for custom matchers against a class). We could express more types of operator-based checks, like:

match(val) {
   when < 10: console.log("small");
   when >= 10 and < 20: console.log("mid");
   default: "large";
}

It would be nice to have this too:

if (val  >= 10 and < 20) console.log("mid");

A similar thing exists in c#.

ljharb commented 2 months ago

if is normal expression space, so adding that ability would need to be its own distinct proposal.

If we're able to add the is operator, though, then you could do if (val is >= 10 and < 20).

oleedd commented 2 months ago

Where is the proposal with the is operator?

ljharb commented 2 months ago

this one. it's the third bullet point in https://github.com/tc39/proposal-pattern-matching?tab=readme-ov-file#specification - but it's not yet clear if we'll be able to keep it or not.