-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
add deferScoped
, library defined scoped defer
#16048
Conversation
defer
defer
deferScoped
, library defined scoped defer
f55cd69
to
7c53157
Compare
7c53157
to
b699dda
Compare
deferScoped: close(a) # immediately follows code that needs cleanup | ||
do: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh please. This is even worse than defer
. There is no visual indication that the do:
block belongs to the deferScoped
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no visual indication that the do: block belongs to the deferScoped.
you don't have to use do
in the common case of 1-liner cleanup
code, eg this works, and shows that visual indication:
echo "before"
deferScoped(echo "cleanup"):
echo "inner"
echo "before"
deferScoped(echo "cleanup"):
echo "inner"
but do
is part of the language and users should learn how it works (and you could use same argument for other API's using do)
The whole point is it doesn't escape the template/macro scope it's called from, unlike defer
template fn():
prelude()
deferSCoped: cleanup()
do:
inner()
fn()
# `cleanup` called before the next statement
fn()
# `cleanup` called before the next statement
I'm seriously sorry but no. Upcoming PRs should simply use |
The |
Heck, I like Python's |
A lot of code (in compiler, stdlib, nimble) is incorrect wrt exception handling as mentioned here https://forum.nim-lang.org/t/4022#44758
however there's some pushback for using
defer
more widely in compiler sources https://forum.nim-lang.org/t/4022#44770and there are legitimate cases where defer in a scope is preferable.
This PR adds a library defined scoped
defer
. The semantic differences wrtdefer
have been explained in #16010 and this in no way makes builtindefer
(aka D'sscope(exit)
) obsolete; just different use cases, with, yes, some overlap.example
links
why not just use try/finally?
as explained in nim-lang/RFCs#236 (comment)
notes
std/defers
because modules are cheap (add a benchmark (to run manually) to test compilation speed in several settings #14614) but re-exported in std/sugar (which is a heavier kitchen sink-like dependency that is likely to keep growing), so that client code can use this without sugar dependency if needed.