Something that would be simple to change and would improve the readability imo would be to add the case keyword at the beginning of the line. That might only be my brain, but I think it would make it slightly easier to skim over the code and understand what happens. It also match (eh 🙂) nicely with switch statements, which might help teaching the feature.
const val = match (res) {
case {status: 200, headers: {'Content-Length': s}} =>`size is ${s}`,
case {status: 404} => 'JSON not found',
case {status} if (status >= 400) => throw new RequestError(res)
};
While I am not in favor of using case I am in favor of the pipe operator from OCaml:
let imply v = match v with
(true,true) -> true
| (true,false) -> false
| (false,true) -> true
| (false,false) -> true
The example above, is a very basic example that shows it's usage. It has way more power in other situations such as checking character types:
let char_discriminate c = match c with
'a' | 'e' | 'i' | 'o' | 'u' | 'y'
| 'A' | 'E' | 'I' | 'O' | 'U' | 'Y' -> "Vowel"
| 'a'..'z' | 'A'..'Z' -> "Consonant"
| '0'..'9' -> "Digit"
| _ -> "Other" ;
Similarity to switch makes it harder to teach imo - i think case should be avoided.
heads-up: I've got a PR over at #79 that would block this from happening in the first place.
I'm also pretty opposed to OCaml/Haskell-style | for this. See previous discussions on compound matchers.
Do you have a link to the discussion? This is a very powerful pattern matching tool, would be a shame to miss out on it.
I think there's enough pushback that we can close this issue, but still, I find the current syntax quite hard to mentally parse, and we're only dealing with relatively simple mono-line cases.