wmsbill / proposal-catch-guards

TC39 proposal for catch guards
MIT License
16 stars 3 forks source link

Why not try..match? #10

Open Hadaward opened 5 months ago

Hadaward commented 5 months ago

I know it might be a little confusing at first but I don't think the original syntax is bad, we could just do something like try..match to specify that "error" instantiates a certain class.

try {
  // some error producing code
} match (TypeError error) {
  console.error(`We got a type error: ${error}`);
} catch (error) {
  console.error(`We got an unknown error: ${error}`);
} finally {
  // handle shutdown
}

The syntax for match is questionable and debatable, but I prefer to keep the part that we don't have an implicit variable to get the error data, an explicitly named variable is much better.

ljharb commented 5 months ago

That seems like a good alternative syntax to consider once pattern matching’s shape is solidified.

FrameMuse commented 1 month ago

I think it implicitly introduces Typization to JS (or at least it looks like it, which adds ambiguity), I would also wanted it but not this way. The more JS-style would be something like this (IMHO)

try {
  // some error producing code
} catch (error; error instaceof TypeError) {
  console.error(`We got the known error: ${error}`);
} catch (error; error instaceof SyntaxError) {
  console.error(`We got another known error: ${error}`);
} finally {
  // handle shutdown
}

That would also allow matching any boolean value, which is more flexible.

ljharb commented 1 month ago

what is "typization"?