Skip to content
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

Optional Catch Binding (Stage 4) #7

Open
bakkot opened this issue Jul 6, 2017 · 14 comments

Comments

@bakkot
Copy link

commented Jul 6, 2017

Champion: @michaelficarra
Spec repo: https://github.com/michaelficarra/optional-catch-binding-proposal/
Spec text: https://michaelficarra.github.io/optional-catch-binding-proposal/
Stage: -1 :)

Moved to Stage 3 at the July meeting https://github.com/tc39/agendas/blob/master/2017/07.md

Stage 4 at May 2018 meeting

This hasn't been presented to the committee as of this writing, but will be at the July meeting. If it receives broad support it is likely to advance quickly, in my personal estimation.

It allows the binding to be omitted from a catch clause:

try {
  throw 0;
} catch {
  doSomethingWhichDoesntCareAboutTheValueThrown();
}

or

try {
  throw 0;
} catch {
  doSomethingWhichDoesntCareAboutTheValueThrown();
} finally {
  doSomeCleanup();
}

This will need a (very small) change in Babylon, including a change to the AST (marking the catch binding as optional), and then a very small change to Babel.

This would make a great first contribution, I think!

UPDATE:

@bakkot

This comment has been minimized.

Copy link
Author

commented Jul 6, 2017

Note that you may want to hold off work until after the July TC39 meeting, in case this gets shot down.

J/k you should do it now :)

@michaelficarra

This comment has been minimized.

Copy link

commented Jul 7, 2017

Nah, this proposal's just going to glide right through to stage 4 as-is. Send in your PRs!

@hzoo hzoo added the Proposal Issue label Jul 10, 2017
@MarckK

This comment has been minimized.

Copy link
Member

commented Jul 13, 2017

Hi, I'd be quite happy to work on this :)

@xtuc

This comment has been minimized.

Copy link
Member

commented Jul 13, 2017

@MarckK good news 👍 . Don't hesitate to ping us on Slack if you need help.

@reaktivo

This comment has been minimized.

Copy link

commented Jul 21, 2017

What do you think of making the catch block optional?

@ljharb

This comment has been minimized.

Copy link
Member

commented Jul 21, 2017

just try { }?

See https://twitter.com/ljharb/status/861992651423227904 for a related thread.

@reaktivo

This comment has been minimized.

Copy link

commented Jul 21, 2017

Yes, seems like the thread supported it. It's useful in cases like the following:

const safeParseJson = (str) => {
  try {
    return JSON.parse(str);
  } catch (e) {}
}

When you actually wanted undefined to be returned. I wish do try would work without brackets.

const safeParseJson = str => do try { JSON.parse(str) };
@hzoo

This comment has been minimized.

Copy link
Member

commented Jul 21, 2017

babel/babylon#634 is merged, will do a release so the transform can use it

@existentialism

This comment has been minimized.

Copy link
Member

commented Jul 25, 2017

babel/babel#5956 (comment) has also been merged

@hzoo

This comment has been minimized.

Copy link
Member

commented Jul 27, 2017

Released in https://github.com/babel/babel/releases/tag/v7.0.0-alpha.17 via babel-plugin-transform-optional-catch-binding

Input

try {
  throw 0;
} catch {
  console.log("it failed, but this code executes");
}

Output

try {
  throw 0;
} catch (_unused) {
  console.log("it failed, but this code executes");
}
@hzoo hzoo changed the title Implement the optional catch binding proposal Optional Catch Binding (Stage -1) Jul 27, 2017
@hzoo hzoo changed the title Optional Catch Binding (Stage -1) Optional Catch Binding (Stage 3) Jul 27, 2017
@hzoo hzoo referenced this issue Jul 28, 2017
10 of 10 tasks complete
@bakkot bakkot referenced this issue Jul 28, 2017
5 of 5 tasks complete
@jridgewell

This comment has been minimized.

Copy link
Member

commented Jul 28, 2017

@MarckK: Want to submit a change to babel-preset-stage-3 to include optional catch binding? Other than that, we can close this proposal.

@hzoo

This comment has been minimized.

Copy link
Member

commented Aug 9, 2017

@existentialism did this in babel/babel#6032 (Stage 3)

Can add to the readme now though.

When should we close (was thinking when its Stage 4)

https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-optional-catch-binding

@pronebird

This comment has been minimized.

Copy link

commented Feb 14, 2018

Sorry if a little bit out of context but the comment by @reaktivo looks really appealing to me.

Maybe this transform wouldn't be needed at all then because not catching errors is a bad practice anyway but if there is the case then maybe it's better to do it differently.

Maybe something like try? or do try or any kind of that stuff could be adopted:

const isTheFeatureImplemented = try? doSomethingThatThrows(); // either result or undefined

or

const isTheFeatureImplemented = try doSomethingThatThrows() catch(e) {
  return 'sayonara'; // quit the scope cuz it's game over
};

Currently this is a big PITA of using exception because there are no guard statements that we could easily use to try or bail.

So what I see is we get a bunch of let+try+catch uses, then everything has to be mutable or need to do const reassignment or any other kind of voodoo.

The other malice practice is people simply wrap all kind of code into one huge try+catch and do everything inside of the try block which is not optimal esp. given that error handling in Javascript is quite infantile.

@ljharb

This comment has been minimized.

Copy link
Member

commented Feb 14, 2018

Optional catch binding is stage 3; this transform is needed regardedless.

@hzoo hzoo changed the title Optional Catch Binding (Stage 3) Optional Catch Binding (Stage 4) May 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
10 participants
You can’t perform that action at this time.