Moritz FS
Moritz FS

Reputation: 51

@animate changes the scope of variables

I am struggling with a weird error. I'm new to Julia so maybe I don't understand something.

Consider the code:

using Plots;
xyz = 1
anim = @animate for (j, iterated_variable) in enumerate(1:10)
    xyz = xyz
    plot(1,1)
end

it will yield the error "UndefVarError: xyz not defined"

while

xyz = 1
anim = @animate for (j, iterated_variable) in enumerate(1:10)
    print(xyz)
    plot(1,1)
end

will run and (oddly enough) print exactly:

111111

1111

where the digits 1,2,7,8,9,10 are printed in monospace and the others in the regular font.

Removing the @animation handle makes the code do what you expect it to do.

xyz = 1
for (j, iterated_variable) in enumerate(1:10)
    print(xyz)
    plot(1,1)
end

and it will output

1111111111.

This error is quite frustrating I must admit, especially since I really am starting to like Julia. Any idea of what is happening?

Julia 1.6.3, VSCodium 1.66.1, Julia language support v1.6.17, notebooks

(edited the question, there was a code mistake)

Upvotes: 2

Views: 78

Answers (1)

Giovanni
Giovanni

Reputation: 293

If by notebooks you mean Pluto.jl notebooks and you run the first snippet all in one cell moving using Plots in a different one should fix the issue

Upvotes: 0

Related Questions