Variabile scope into the for? panic

I am trying Julia since 3 weeks, so i am totally beginner. Here i was trying to get argument passed to my script, running into the command line:

folder_path = “”
println(“Set parameters …”)
for i in 1:length(ARGS)
x = ARGS[i]
println(“x=”*x)
if x == “-f”
global folder_path = ARGS[i+1]
end
end

Amazingly, I see that from inside the for loop I don’t see the variable folder_path if i do not put “global” in front of the var ?!

Variable scoping in Julia are so drastic?
I am thinking … oh … how many time i will code wrong to remember me this scoping rules!

I’m used to the fact that if I define a variable somewhere in the code, it will then be visible to the rest of the code …

Newlisp shocked me when I tried it, because the variables had a general scope even if declared inside the functions (!?) … here we are the opposite … I understand that the code is more secure and more separate … but it is mentally demanding that remind me that I don’t see external variables inside a for loop …

Panic … or I misunderstood?

Hi there and welcome! How are you learning Julia? - this is covered in the manual quite well I think.

Hi Dario,

look, this is not so bad as you think. If you get used to write functions, this does not matter anymore:

function dario(folder_path="abc")
    for i in 1:3
        folder_path *= string(i)
        println("$i: $folder_path")
    end
end

julia> dario()
1: abc1
2: abc12
3: abc123

allora, no need to panic :slight_smile:

In addition to the comments above this only applies to code executed in the global scope (e.g., from the REPL as you are doing). If you do this from within a function then it works as expected.

This was the subject of quite a lot of discussion and this behaviour has recently been change (for the REPL only) - see https://github.com/JuliaLang/julia/pull/33864 which I presume will become v1.5 in due course.

3 Likes

Damn! a function with my name makes a certain effect!

Thanks, il will use functions, the max i can.

1 Like

I have tried (also if nobody here is called foo :slight_smile: ) i this way:


> function pippo()
>  x = 1
>  for i in 1:10
>   x = i
>  end
>  println("x=", x)
> end
> 
> y = 1
> for i in 1:10
>  y = i
> end
> 
> println("y=", y)
> 
> pippo()
julia> 

y=1
x=10
julia>

Ok, so, in the function the scope is as i think it must be … ok, i will use functions, before to have this in the 1.5 release i think is better to enter this mentality. I think they will be right to change this behaviour because it is not it is not so homogeneous to have scoping different in REPL and in functiosn … but i think, there must have been valid reasons for doing so.
Ok thanks.

Yes, i have to read a manual, you are right … but, you know, when you test if a language is for you, and you do not have so much time, sometimes you go ahead here and there … avoiding a systematic reading that often takes a long time.

But now it’s time to read a manual … because … Julia has convinced me enough for now … so it’s time to start a sistematic reading …

2 Likes

:grinning: it’s partly that there are some out-of-date resources at large on the internet and it would be good to correct them if possible. (For example, I have to update the Wikibook occasionally when people report problems.)

1 Like

No need to read end to end quite yet.

I would begin in Jupyter notebooks if you are just starting. It does not have the same issue you ran into (ie you wouldn’t have needed global there) and I think you will find it very intuitive there.

Thanks @jperla.

Jupiter … mmm, yes I saw it in the videos … but I’m not a researcher … for now I’m using Juno with satisfaction … and then I’m interested in launching julia from the command line, to do scripting (scripting yes, but compiled, I mean. …) and some fast elaboration on texts. So, i have necessarily to focus on run Julia in command line …

How strange though … that is, it seems that inside a for loop, however, external objects are visible, while simple variables are not … if I do:

mutable struct Ttest
  value
end

t = Ttest(0)

b = 1
t.value = 1

for i in 1:10
  b = 2
  t.value = 2
end
println(b)
println(t.value)

i get this

julia> 
1
2
julia>

b is again 1, while the var declared as struct, has changed

Thanks, i will get your pdf :slight_smile:

You don’t have to be a researcher at all. It is a nice interactive environment to start in, easy to install, and doesn’t have the confusing scoping gotchas that you hit.

Not absolutely necessary, but by the time you want to move back to juno these things will be very intuitive

I think to have found my definitive solution: in the script inside.jl that i want to run from command line,
i will do simply a “main()” function, and i will have no problems of scope!

function main()
  @show ARGS
  b = 4
  function change_inside()
    b = 5
  end
  change_inside()
  println("b inside is ", b)
end

main()

i get, running in command line:

c:\vmswap\juliaPrj\test>julia inside.jl 1 2 3
ARGS = ["1", "2", "3"]
b inside is 5

c:\vmswap\juliaPrj\test>

so no problem in scoping. :smiley:

2 Likes

However, i will install Jupiter, ok.

have you tried SoftGlobalScope?

Thanks @Paul_Soderlind, i had a look the link you pass me … but perhaps i prefer to put myself into “more function” mentality, now that i have a (little) better understand on the Julia REPL behaviour. The important thing is to have understood clearly it, to not fall easily in bugs due to it. :slight_smile:

1 Like