-
Notifications
You must be signed in to change notification settings - Fork 89
Examples with try/catch #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't have any examples, as I'm not sure how the current proposal could look. I think this is an important use case to consider. I am hoping we can have a binary operator that takes a Pattern for it's RHS, so you could do |
@bterlson Ah, that seems useful to have an expression form. I was thinking of a statement form that could be unified across As a motivating example, in the Rust programming language there is basic control flow like if my_bool { ... }
while my_bool { ... } Additionally, there are pattern matching forms for both of these. The pattern matching forms let you check if a pattern matches and destructure the pattern if it does. if let Ok(value) = my_result { ... }
while let Some(value) = my_iter.next() { ... } Back to JavaScript, I could imagine a similar sort of construct (don't focus on the exact syntax too much 😸) if (let { x, y, z } = value) { ... }
while (let { done: false, value } = iter.next()) { ... }
try { ... } catch (let { x, y, z } = value) { ... } |
I can see statement forms being useful but I'm not sure how they should look. It should at least be clear that pattern matching semantics are used (rather than normal destructuring assignment semantics which I would probably expect in your examples above). |
Oops, that's an obvious mistake on my part. Let me trade that strawman in for another if (match { x, y, z } = value) { ... }
while (match { done: false, value } = iter.next()) { ... }
try { ... } catch (match { x, y, z } = value) { ... } |
I'm admittedly over my head when it comes to grammar definitions, but is it possible to make try {
...
} catch (err) {
TypeError: /* handle it */,
MyCustomError: /* handle it */,
else: /* ... */
} So, the
(forgive me, people who actually understand how I bludgeoned the above...) |
@jmar777 that particular syntax would be unlikely on the grounds that it wouldnt be backwards compatible. More likely would probably be multiple
(though |
Ugh, I always forget about labels. I think my example above would only be valid using the new grammar rules, but yeah, a single match expression would be syntactically equivalent to |
Hey y'all! #65 has gotten merged, and a lot of issues have become irrelevant or significantly changed in context. Because of the magnitude of changes and subtle differences in things that seem similar, we've decided to just nuke all existing issues so we can start fresh. Thank you so much for the contributions and discussions and feel free to create new issues if something seems to still be relevant, and link to the original, related issue so we can have a paper trail (but have the benefit of that clean slate anyway). As far as this particular suggestion goes: there's a Beyond This Spec section that explicitly mentions |
Firefox implements non-standard conditional catch clauses like
I think this is a useful shorthand as it helps avoid accidental trolls with forgetting to rethrow errors you're not interested in.
The reason I bring it up in this proposal is that it is often mentioned that pattern matching is in some sense a blocker for standardizing conditional catches. e.g. here or here.
Do you see a path for applying this proposal to conditional catches?
The text was updated successfully, but these errors were encountered: