Not sure if this has already been considered, but I find it confusing that the case keyword (which is used for each branch in the switch statement) is used as the starting keyword for this new construct, and a new when keyword is introduced for the branches.
I'd find it more intuitive and regular to introduce a new keyword to start the construct, eg match, and use case for each branch:
match (res) {
case {status: 200, headers: {'Content-Length': s}}:
console.log(`size is ${s}`)
case {status: 404}:
console.log('JSON not found')
case {status} if (status >= 400): {
throw new RequestError(res)
}
}
Moreover, to be consistent with switch, one could also use : instead of -> at the end of patterns.
@ljharb (for one) is staunchly against similarities to switch. I think this would otherwise be a fine syntax, but the overlap could be confusing when the semantics are so wildly different.
(Fwiw as a lurker, I'm also +1 for match over case ("because scala" + "the word 'match' is even in the name of the proposal) but I feel like this has probably been explained ~N times already about why case over match is necessary/superior/etc, but I haven't seen it (?).
I swear I scan for this "case vs. match" rationale ~once every few months or so and, so far, haven't found it. Given the popularity of this proposal + passion lurkers will have around such a potentially hugely useful feature, it might be worth an FAQ entry for this sort of "case vs. match stuff. I swear I've looked for such, and know this is a "lazy ask" vs contributing this via a PR, and of course apologies if I'm missing where this is already obviously laid out...)
(I also acknowledge the naming of match or case is probably one of the easiest things to comment on, vs. say the nuanced semantics of binding cases/etc., but hey, that's the definition of bikeshedding, so...)
One of my most important constraints is that there can be no overlap between pattern matching and switch. This proposal will be updated in the medium term, and won't use the case keyword.
Most helpful comment
One of my most important constraints is that there can be no overlap between pattern matching and switch. This proposal will be updated in the medium term, and won't use the
casekeyword.