# 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:** 3\
**Page:** 2

<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).

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 18, 2025, 6:02am UTC](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438/22 "2025-09-18T06:02:51Z")

</div>

> [@WalterMadelim](#):
>
> I can see that the consequence of using `$` is the cut-and-dried static variable `%14`, which is later again assigned to `%21` and `%23`. Therefore, the snapshot _value_ of `f(g(j))` is **already decided** when the respective `task` is **created**.

Using interpolation, i.e. `$`, in various macros is literally a rewriting to compute the interpolated expression before whatever the macro does. That is,

```julia-auto
@spawn something($expr)

```

is **literally** rewritten to something like

```julia-auto
...
let uniquename=expr
    t = Task(() -> something(uniquename)
end
...

```

This is done by `Base._lift_one_interp!(...)`, which works on the syntax tree produced by the parser.

Likewise, a `for` loop is **literally** rewritten to a `while` loop using `iterate(...)`, as described above, and in the docs. Though with unique variable names which your program can’t touch, so it can’t be inadvertently mutated. (Except in an outer `for`). The result of these rewriting operations can be observed e.g. with the `@code_lowered` macro, and for the interpolation with a `@macroexpand`:

```julia-auto
julia> @macroexpand Threads.@spawn foo($i)
quote
    #= threadingconstructs.jl:511 =#
    let var"##277" = i
        #= threadingconstructs.jl:512 =#
        local var"#70#task" = Base.Threads.Task((()->begin
                            #= REPL[47]:1 =#
                            foo(var"##277")
                        end))
...

```

---

<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 18, 2025, 7:01am UTC](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438/23 "2025-09-18T07:01:22Z")

</div>

I have a new point of confusion. Open a new topic…

[Previous page](https://discourse.julialang.org/t/why-wont-julia-error-if-a-variable-is-not-defined-when-scheduling/132438.md?page=1)
