Open johnnovak opened 2 years ago
var deferred: proc()
for i in 1..3:
if i == 1:
deferred = proc() = echo i
deferred() # 3
sugar.capture seems to be created for this.
Okay, so closures capture variables by reference... Well, that's news to me. I always thought they capture the current values! (so they make a copy)
Any captured variables are stored in a hidden additional argument to the closure (its environment) and they are accessed by reference by both the closure and its enclosing scope (i.e. any modifications made to them are visible in both places).
But in any case, shouldn't my example then just use the value of the last iteration? It still seems to be a bug, why does it crash?
The value of the last iteration is Item(kind: ikTwo, two: false)
. The closure tries to obtain the one
field from this object, but this field is not accessible since its kind is not ikOne
.
What the title says... Probably easier to understand the problem by reading this example carefully, as it seems to be quite specific:
Example
Current Output
Expected Output
Additional Information
Tried with all memory management options, except for
go
, and I got the same results every time.Also tried with 1.6.0 -- same results.
Tried a few variations (
var
instead oflet
, usingmitem
, using local vars instead of globals), but the error always happened. In any case, this is a valid scenario and it should work.