# REPL and for loops (scope behavior change)

**URL:** <https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514>\
**Category:** General Usage\
**Created:** [August 15, 2018, 5:26pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514 "2018-08-15T17:26:35Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 25, 2019, 1:11am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/21 "2019-01-25T01:11:42Z")

</div>

Just use [IJulia](https://github.com/JuliaLang/IJulia.jl). It will have the same “feel” as v0.6.  
Also, it wouldn’t hurt just trying to learn the new scoping rule…

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 25, 2019, 1:12am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/22 "2019-01-25T01:12:17Z")

</div>

> [@Paul](#):
>
> pointing me to THE helpful resource

Or more generally, use [GitHub - JuliaLang/SoftGlobalScope.jl: utilities for "soft" global scope in interactive Julia environments](https://github.com/stevengj/SoftGlobalScope.jl). Put it in your startup file if you wish.

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://discourse.julialang.org/u/Paul)\
**Post date:** [January 25, 2019, 1:30am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/23 "2019-01-25T01:30:50Z")

</div>

IJulia does not give me the speed that I need for my calculations. I need a local installation with a lot of help from the GPU. That’s why I ended up going from IJulia (0.x) notebooks on the web to the desktop. That’s how I landed with JuliaPro 1.x

I got some help with scoping a simple loop this morning. Immediate success. I was happy. Then I added another loop to the program. The same recipe does not work. I’d have to go into computer science mode to get over the hump. That hurts a lot. I don’t think like a programmer and I don’t want to learn how to think like a programmer. I’d rather be waterboarded, right now.

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://discourse.julialang.org/u/Paul)\
**Post date:** [January 25, 2019, 1:38am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/24 "2019-01-25T01:38:31Z")

</div>

Thanks for this. I’ll check it out. If SoftGlobalScope.jl does the job, I’ll be back and we’ll have a party. I have to say that in the meantime, I’ve already got a body part or two in the jaws of Mathematica. Wish me luck.

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://discourse.julialang.org/u/Paul)\
**Post date:** [January 25, 2019, 1:41am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/25 "2019-01-25T01:41:41Z")

</div>

Thanks for the interest. My original problem lives here: [Global Scope Confusion](https://discourse.julialang.org/t/global-scope-confusion-again/20044)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 25, 2019, 1:59am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/26 "2019-01-25T01:59:28Z")

</div>

> [@Paul](#):
>
> IJulia does not give me the speed that I need for my calculations. I need a local installation with a lot of help from the GPU.

You can run IJulia locally. Don’t confuse IJulia with juliabox or whatever cloud server you are using.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 25, 2019, 2:01am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/27 "2019-01-25T02:01:55Z")

</div>

> [@mbauman](#):
>
> Or more generally, use [https://github.com/stevengj/SoftGlobalScope.jl](https://github.com/stevengj/SoftGlobalScope.jl). Put it in your startup file if you wish

SoftGlobalScope doesn’t work in the REPL. It only works in environments that choose to use it, which so far is just IJulia.

> <https://github.com/JuliaLang/SoftGlobalScope.jl/issues/2>
>
> As suggested by @dawbarton, in https://github.com/JuliaLang/julia/issues/28789#i…ssuecomment-415063358, it is possible to hack the active REPL to use SoftGlobalScope. Would be nice to add this feature here, so that \`using SoftGlobalScope\` patches the active REPL if there is one.
> 
> Similar \[to this code in OhMyREPL.jl\](https://github.com/KristofferC/OhMyREPL.jl/blob/340bb0e3c63b5f46aee9f14d3f2665465c0a51db/src/OhMyREPL.jl#L79-L110), we would do this in \`\_\_init\_\_\` only in interactive mode.
> 
> @KristofferC, your advice on the cleanest way to patch the REPL's \`eval\` loop would be appreciated.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 25, 2019, 2:03am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/28 "2019-01-25T02:03:50Z")

</div>

Ah, thanks for the correction. I had thought it did.

---

<div class="post-metadata">

**Author:** ![Paul](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Paul](https://discourse.julialang.org/u/Paul)\
**Post date:** [January 25, 2019, 2:47am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/29 "2019-01-25T02:47:59Z")

</div>

Aahhh. That sounds promising. Thanks.😀

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 25, 2019, 2:54pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/30 "2019-01-25T14:54:48Z")

</div>

This is slightly off topic, but I never liked much having jupyter notebooks in the middle of my browsing tabs.  
I like [nteract](https://nteract.io), which provides an isolated desktop app for jupyter notebooks.  
It is beta, though, so you can expect some bugs lurking around.  
In Unix plataforms it is able to “self-setup” it to look for installed jupyter kernels. I don’t know if it work as easily on Windows.

---

<div class="post-metadata">

**Author:** ![ericagol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericagol/32/5392_2.png) [@ericagol](https://discourse.julialang.org/u/ericagol)\
**Post date:** [January 24, 2020, 6:16pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/31 "2020-01-24T18:16:22Z")

</div>

One aspect of Julia I enjoy is being able to test code in the REPL, and then examine or plot variables, arrays, or vectors after I run a bit of code to see whether it ran as expected. Sometimes I don’t know which variables I would like to examine, and so having these available in the global scope can help immensely in debugging. Using a “let” closure does not allow me to examine the variables after the fact. Is there some means of, say, returning all variables from a function (or a let block) so that I can see them in global scope?

---

<div class="post-metadata">

**Author:** ![Dustin\_Hess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dustin_hess/32/11957_2.png) [@Dustin\_Hess](https://discourse.julialang.org/u/Dustin_Hess)\
**Post date:** [May 15, 2020, 8:44pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/32 "2020-05-15T20:44:28Z")

</div>

Hi I read through scope rules. These still don’t make very much sense to me. If someone could please explain.I will give a couple examples below.

Fist the classic example a variable to keep track of iterations

```julia
x = 0
for i in 1:5
    x+=1
end

```

Gives an " x not defined" ( out of scope).  
Yet I can do the below just fine.

```julia
a = [1,2,3,4]
b = zeros(Int, 4)

for (i, x) in enumerate(a)
    b[i] = x
end

```

None of these variables are in functions and therefore should be in the global scope. According to docs loops are locally scoped so this shouldn’t work. If the top example doesn’t work I am baffled why the second example would work?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 15, 2020, 8:53pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/33 "2020-05-15T20:53:24Z")

</div>

This will change in Julia 1.5 to act like you expected.

The difference between the two examples is that your first one is changing what the name `x` is used for, whereas the second is changing what value in stored in `b[i]`.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [May 15, 2020, 8:54pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/34 "2020-05-15T20:54:30Z")

</div>

Contrary to appearances, the second example doesn’t involve assignment. Instead it’s lowered to `setindex!`:

```julia
julia> Meta.@lower b[i] = x
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ Base.setindex!(b, x, i)
└── return x
))))

```

The rule that’s being violated by the first one from [the scope section of the manual](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables-1) is:

> An explicit global is needed to assign to a global variable

Since no global variable is being assigned to in the second example, the rule doesn’t apply.

As noted above, this is set to change _in the REPL_ for v1.5, but the behavior will still be the same in global scope within scripts and packages, so understanding the difference is still important.

---

<div class="post-metadata">

**Author:** ![Dustin\_Hess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dustin_hess/32/11957_2.png) [@Dustin\_Hess](https://discourse.julialang.org/u/Dustin_Hess)\
**Post date:** [May 15, 2020, 9:48pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/35 "2020-05-15T21:48:12Z")

</div>

@non-Jedi  
Thank you for the explanation. That makes sense. The confusion arises because the x = 0 being an assignment. I understand it but it is not intuitive. This also leads to more confusion because I can do

```julia
x = 0
x[0] # returns the value of x as 0

```

Yet I cannot mutate x by x[1] = 1. It seems to me you should be able to mutate this since the getindex works. However obviously this not an array. That being the case getindex should not work. Coming from other languages that have separate syntax to denote assignment, makes this ambiguous about what is meant “=” and in this case what is an array and what is not.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 16, 2020, 6:11am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/36 "2020-05-16T06:11:33Z")

</div>

> [@Dustin\_Hess](#):
>
> It seems to me you should be able to mutate this since the getindex works.

`getindex` does not imply `setindex!` — there are immutable arrays. Prominent examples include

> **[GitHub - JuliaArrays/StaticArrays.jl: Statically sized arrays for Julia](https://github.com/JuliaArrays/StaticArrays.jl)**
>
> Statically sized arrays for Julia. Contribute to JuliaArrays/StaticArrays.jl development by creating an account on GitHub.

> **[GitHub - JuliaArrays/FillArrays.jl: Julia package for lazily representing...](https://github.com/JuliaArrays/FillArrays.jl)**
>
> Julia package for lazily representing matrices filled with a single entry - GitHub - JuliaArrays/FillArrays.jl: Julia package for lazily representing matrices filled with a single entry

and many others.

> [@Dustin\_Hess](#):
>
> This also leads to more confusion because I can do
> 
> ```julia
> x = 0
> x[0] # returns the value of x as 0
> 
> ```

I am wondering if you misread something, since

```julia
julia> x = 0
0

julia> x[0]
ERROR: BoundsError
Stacktrace:
 [1] getindex(::Int64, ::Int64) at ./number.jl:78

```

The only index that works is `x[1]`. That comes from a decision to make numbers iterable containers with a single element — it is controversial, but because of compatibility it won’t be changed before 2.0.

---

<div class="post-metadata">

**Author:** ![Dustin\_Hess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dustin_hess/32/11957_2.png) [@Dustin\_Hess](https://discourse.julialang.org/u/Dustin_Hess)\
**Post date:** [May 16, 2020, 3:56pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/37 "2020-05-16T15:56:43Z")

</div>

@Tamas_Papp  
Thanks for the reply. Yes ok its starting to make sense now. I did some more reading based on your reply. It’s been very helpful.

---

<div class="post-metadata">

**Author:** ![goodluck](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@goodluck](https://discourse.julialang.org/u/goodluck)\
**Post date:** [September 16, 2020, 11:16am UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/38 "2020-09-16T11:16:31Z")

</div>

“… changing what the name `x` is used for…”

i would disagree, in both cases X is used as a variable, albeit a different kind of variable  
distinguishing scope behavior on “kind” of variable is highly surprising

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 16, 2020, 12:58pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/39 "2020-09-16T12:58:03Z")

</div>

This is an old thread. You should try out Julia 1.5 — there things should “just work” as you want them to.

> [@goodluck](#):
>
> in both cases X is used as a variable, albeit a different kind of variable

I personally find the traditional mental model of “variables” — that is, boxes that you put things in and out of — to be quite unhelpful for understanding Julia’s behaviors. It’s much more consistent to simply think of them as names. Re-assignment (`x = 1`) is simply stating that you now have a better use for a particular name; it is not a mutation.

Locations in an array, however, are precisely boxes with things in them.

---

<div class="post-metadata">

**Author:** ![goodluck](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@goodluck](https://discourse.julialang.org/u/goodluck)\
**Post date:** [September 16, 2020, 1:15pm UTC](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/40 "2020-09-16T13:15:13Z")

</div>

thanks

im on a mac, installed via the official process  
julia\> VERSION  
v"1.5.1"

but, sounds great that the thing should work as explained.  
then, can anybody verify this as a bug on their mac / system?

[Previous page](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514.md?page=1)

[Next page](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514.md?page=3)
