# Scope and global in REPL (julia 0.7)

**URL:** <https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233>\
**Category:** Internals & Design\
**Tags:** repl, scope\
**Created:** [April 8, 2018, 7:00pm UTC](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233 "2018-04-08T19:00:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 8, 2018, 7:00pm UTC](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233/1 "2018-04-08T19:00:53Z")

</div>

```julia
x=1;
while x<10
    x+=1
end
┌ Warning: Deprecated syntax `implicit assignment to global variable `x``.
│ Use `global x` instead.
└ @ nothing none:0

```

I think while should not open a new scope of variables. In more complex real world code this would force us to add many global (or local) statements for every variable which is altered inside the loop.

Another one where I am unhappy about:

```julia
a=zeros(Int,5)
i=1
a[i]=1
for i in 2:5
	a[i]=i
end

```

gives:

```julia
┌ Warning: Deprecated syntax `implicit assignment to global variable `i``.
│ Use `global i` instead.
└ @ nothing none:0
┌ Warning: Loop variable `i` overwrites a variable in an enclosing scope. In the future the variable will be local to the loop instead.
└ @ nothing none:0

```

Again, the “global i” is annoying, but using it:

```julia
a=zeros(Int,5)
i=1
a[i]=1
for i in 2:5
	global i
	a[i]=i
end

```

still gives:

```julia
┌ Warning: Loop variable `i` overwrites a variable in an enclosing scope. In the future the variable will be local to the loop instead.
└ @ nothing none:0

```

and this is even unclear how to avoid this warning.

Is there something I miss? I am the only one who is wondering why the above scope examples are a good thing?

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [April 8, 2018, 7:12pm UTC](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233/2 "2018-04-08T19:12:56Z")

</div>

> [@oheil](#):
>
> In more complex real world code this would force us to add many global (or local) statements for every variable which is altered inside the loop.

The crucial thing is `global` issue. In real world you will probably have loop along with the variable it references to defined inside a function and in such a situation `while` loop will just work and in `for` loop there is `outer` keyword that handles this case, see [https://docs.julialang.org/en/latest/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1](https://docs.julialang.org/en/latest/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1).

Finally, if you do not want to define a function you can use `let` block e.g.:

```julia-auto
julia> let
       x=1;
       while x<10
           println(x); x+=1;
       end
       print(x)
       end
1
2
3
4
5
6
7
8
9
10

```

EDIT:  
One more comment about `let` block.

This form:

```julia-auto
let x = 1
end

```

creates new binding for `x` that is visible only inside `let` block, but this form:

```julia-auto
let
x = 1
end

```

will give you the same warning as `for` and `while` loop if `x` was earlier defined in global scope.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 8, 2018, 7:40pm UTC](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233/3 "2018-04-08T19:40:54Z")

</div>

Thanks for the help. Especially the `outer` keyword was completely hidden from my radar.

Still I think the whole scope thing, as it is now in 0.7, will bring a lot of questions in future. As said in the docs:

> The distinction between inheriting global scope and nesting local scope can lead to some slight differences between functions defined in local vs. global scopes for variable assignments.

The implications seems to be subtle and therefor prone to headaches especially for beginners or people coming from other languages.

Anyways thanks for the hints, they solve my special issues I have currently changing from 0.6 to 0.7.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [April 9, 2018, 2:53pm UTC](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233/4 "2018-04-09T14:53:22Z")

</div>

I think part of the difficulty is due to the deprecation warnings. When those are removed, the case where `global i` is declared in the loop can work.

The new rule around this is actually very simple: global variables are not automatically inherited for writing by inner scopes. That’s all.

The other relevant change is that `for i` acts like a repeated version of `let i`; i.e. the loop variable will always be a new variable. This helps avoid action-at-a-distance in code, and makes it easier to switch between for loops and functional constructs.
