generally, I think #418 is a better general-purpose fix. The problem with defer and finally is that we don't run the iterator to completion so not all code is run - this is the same problem as below, without a closure iterator transformation:
iterator x(): int {.closure.} =
yield 42
echo "hello"
proc works() =
var z = x
for a in z():
echo a
proc broken() =
var z = x
for a in z():
echo a
break
works()
broken()
generally, I think #418 is a better general-purpose fix. The problem with
defer
andfinally
is that we don't run the iterator to completion so not all code is run - this is the same problem as below, without a closure iterator transformation: