You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary:
I propose allowing allowing labels for defer statements similar to break statement.
Loop:
for {
for {
break Loop
}
}
The break statement jumps out of both loops here. I propose that we use defer with labels to have defers that get executed when we exit a block. E.g.
funcf() {
.
.
.
Block:
{
deferBlockg()
}
.
.
.
}
the deferred g() will be executed not at the end of function f() but at the end of the block.
E.g. with a lock we can have something like:
Locked:
{
m.Lock()
deferLockedm.Unlock()
.
.
.
}
Background:
Developers want to defer statements to end of block and not just to the end of the function.
Proposal:
Allow defer to get executed at the end of blocks using labels.
Impact:
No impact on existing code.
Discussion:
Defer is an extremely useful feature of go. However deferring to the end of a function is not always the right choice. E.g. unlocking a lock needs to happen as soon as possible and deferring to the end of the function is not a good fit.
Alternatives:
Alternative 1: inline anonymous function
The block of code can be made an anonymous function but make a block an anonymous function just to use defer seems an abuse of anonymous function and is more verbose and can hinder readability of the code.
Alternative 2: helper function
Turning the block into a helper function can make sense sometimes but there are trade-offs, e.g. whether the block makes sense as a named function and the variables in the block will be passed to the function explicitly.
The text was updated successfully, but these errors were encountered:
As you say, you can just use a function literal. This doesn't add any new functionality to the language. It doesn't rise to the level of something that needs to be in the language. Better to keep things simple.
Summary:
I propose allowing allowing labels for defer statements similar to break statement.
The break statement jumps out of both loops here. I propose that we use defer with labels to have defers that get executed when we exit a block. E.g.
the deferred
g()
will be executed not at the end of functionf()
but at the end of the block.E.g. with a lock we can have something like:
Background:
Developers want to defer statements to end of block and not just to the end of the function.
Proposal:
Allow defer to get executed at the end of blocks using labels.
Impact:
No impact on existing code.
Discussion:
Defer is an extremely useful feature of go. However deferring to the end of a function is not always the right choice. E.g. unlocking a lock needs to happen as soon as possible and deferring to the end of the function is not a good fit.
Alternatives:
Alternative 1: inline anonymous function
The block of code can be made an anonymous function but make a block an anonymous function just to use defer seems an abuse of anonymous function and is more verbose and can hinder readability of the code.
Alternative 2: helper function
Turning the block into a helper function can make sense sometimes but there are trade-offs, e.g. whether the block makes sense as a named function and the variables in the block will be passed to the function explicitly.
The text was updated successfully, but these errors were encountered: