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

Allow flat rescue/ensure/else block in do/end block #5114

Conversation

makenowjust
Copy link
Contributor

This feature is imported from Ruby 2.5: https://bugs.ruby-lang.org/issues/12906

Now, we can write such a code:

%w(foo bar).each do |x|
  raise x
rescue e
  p e
end

@RX14
Copy link
Contributor

RX14 commented Oct 13, 2017

Is there any examples where this could be used in the stdlib? I think they would help a lot in making a decision on this PR.

@makenowjust makenowjust force-pushed the feature/crystal/do-end-with-ensure-rescue-else branch from c8b2f73 to ce5bb16 Compare October 13, 2017 12:44
@straight-shoota
Copy link
Member

@ysbaddaden
Copy link
Contributor

My gut reaction was: "oh, looks sweet". It fits with the def/rescue/end syntax we already have. Looking at some of the stdlib examples, I believe it would be nice sugar.

Would the following work?

spawn do
  something
rescue ex
  logger.error(ex)
end

@makenowjust
Copy link
Contributor Author

@straight-shoota Thank you very much!! And we should fix them after merged this and released next version.

@ysbaddaden Yes, of course!

@asterite
Copy link
Member

Can the formatter handle this?

@makenowjust
Copy link
Contributor Author

@asterite Of course. I did't touch formatter code, but formatter worked fine. That's fantastic.

@RX14
Copy link
Contributor

RX14 commented Oct 13, 2017

Would be nice if we cleaned up the places where rescue was in a block in the stdlib.

My first thought was that it doesn't look right because I'm not used to it, but its consistent with the other block types like def and begin, so I think it's fine. Its a bit more visually noisy than the def and begin variants in the language already though.

@RX14
Copy link
Contributor

RX14 commented Oct 13, 2017

Oh, and a formatter spec would be nice even if it wasn't broken before.

@straight-shoota
Copy link
Member

Just a quick thought: What about while? It's used very similar to a block, especially compared to each or loop (which is implemented using while), and perhaps it would be nice if it had similar features...
It could however lead to some misunderstandings regarding break and next. It would be reasonable to assume that both trigger ensure, or only next or maybe even none of them - depending on the context. That's why I'm skeptical, but maybe there are other thoughts about this.

@bew
Copy link
Contributor

bew commented Oct 16, 2017

It could however lead to some misunderstandings regarding break and next. It would be reasonable to assume that both trigger ensure, or only next or maybe even none of them - depending on the context. That's why I'm skeptical, but maybe there are other thoughts about this.

Note: there's also next for blocks

@straight-shoota
Copy link
Member

True. And break also. So this is also relevant with respect to blocks.

@makenowjust
Copy link
Contributor Author

@RX14 Added formatter spec for this.

@asterite
Copy link
Member

I know Ruby is adding this for 2.5, but after thinking about this a bit more, I wonder if this code is clear:

[1, 2, 3].each do |x|
  # do something
ensure
  # clean up
end

Is the ensure executed after all the items, or once for each item? I know it's after each item, but maybe it's a bit confusing. For a method there's no problem because it's done after the whole method. But for a block that can be executed many times, I don't know...

@makenowjust
Copy link
Contributor Author

ensure runs against each items, it seems clear to me because ensure appears inside block and the block runs against each items.

@asterite
Copy link
Member

I still 👍 this

@Brusalk
Copy link

Brusalk commented Oct 19, 2017

My 2c is that I don't find it that obvious (at a glance) that the ensure will be executed for each block invocation. I like the syntax for blocks that'll execute exactly once, for sure.

I think we should be sure to very clearly and explicitly document this behavior.

Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will be super useful for spawn at the very least. And probably others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants