Documentation on for-loops and scoping (in and out of REPL)?

#1

So I can see that issues of scoping are a HUGE subject of debate in different places, and I don’t mean to join the debates over what julia should do.

Rather, I was hoping someone could lay out what the current state is (or, if all these discussions have resolved things, planned changes). I’m really struggling to figure out what behavior to expect right now…

e.g. for loops seem to not have access to variables outside the for loop in the REPL:

julia> i = 0
0

julia> for j in 1:10
           i += j
           end
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at ./REPL[4]:2 [inlined]
 [2] top-level scope at ./none:0

But they do inside a function?


julia> function test()
           i = 0
           for j in 1:10
              i += j
           end
           return i
           end
test (generic function with 1 method)

julia> test()
55

Is this JUST about what happens in the REPL / jupyter notebooks? Is this an example of a more general rule that also applies to scoping of functions, etc. in the REPL / jupyter notebooks?

Again, no desire to join battles over shoulds, just want to understand what behavior to expect now in 1.1.0.

Thanks!

0 Likes

#2

The semantics provided by the language per se is well-documented:

https://docs.julialang.org/en/v1/manual/variables-and-scoping/

But in IJulia, it is overridden for the global scope:

0 Likes

#3

ahhh, ok… that does help clarify a lot, thank you. That difference was very confusing…

1 Like