# Why won't julia ERROR if a variable is not defined when scheduling?

**URL:** <https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438>\
**Category:** New to Julia\
**Tags:** question, task\
**Created:** [September 16, 2025, 12:27pm UTC](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438 "2025-09-16T12:27:43Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [September 16, 2025, 11:50pm UTC](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438/21 "2025-09-16T23:50:49Z")

</div>

> [@WalterMadelim](#):
>
> It would be better if anyone could share a link where this statement is proved.

I come up with these tests to back that statement

```julia
function main()
    v = Function[]
    for i = 10:10:20
        for j = i+3:i+4
            push!(v, () -> println("I'm ", j))
            v[end]()
            j = (i)j # modify!
            v[end]()
        end
    end
    println("---see what we got---")
    map(x -> x(), v)
end
main();

function main()
    v = Function[]
    for i = 10:10:20
        for outer j = i+3:i+4
            push!(v, () -> println("I'm ", j))
            v[end]()
        end
        local j = 6i
    end
    j = -9 # irrelevant
    println("---see what we got---")
    map(x -> x(), v)
end
main();

```

To avoid unintended mutation, do

> [@nsajko](#):
>
> What you can do is create a new task using `j`, instead of capturing `j`. For example:
> 
> ```julia-auto
> let j = j
> Task(() -> work(j))
> end
> 
> ```

Update my knowledge [here](https://discourse.julialang.org/t/is-it-defined-behavior-to-modify-iter-while-for-looping-over-it/132477/21).

---

_[View the full topic](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438)._
