Description
By the current doc, the loop body is viewed as being directly nested both in the innermost nesting function of the loop and in the yield
function which is called in the iterator function.
In my opinion, the loop body should be only viewed as directly nested both in the innermost nesting function of the loop.
In other words, when a function is used as an iterator, the yield
calls should viewed as being removed from the iterator function.
The effect of the doc change is that panics created in loop body are not catch-able in the iterator function.
(Another explanation: the panics created in loop bodies don't propagate to iterator functions. The yield
call just returns a false
when panicking,)
For example, the following two programs should have the same behavior, but they don't in 1.24.0 implementation. This is weird to users. By adopting the above explanations, then their behavior will become the same (the two recover
calls should be both no-op).
package main
import "fmt"
func main() {
defer foo()
}
func foo() {
for range iter {}
panic(123)
}
func iter(yield func(int) bool) {
defer func() {
fmt.Println(recover())
}()
yield(0)
}
package main
import "fmt"
func main() {
defer foo()
}
func foo() {
for range iter {
panic(123)
}
}
func iter(yield func(int) bool) {
defer func() {
fmt.Println(recover())
}()
yield(0)
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Status