# Why does the soft scope warning not appear in a nested loop?

**URL:** https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646
**Category:** New to Julia
**Tags:** question
**Created:** [December 6, 2021, 11:08am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646 "2021-12-06T11:08:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [December 6, 2021, 11:08am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/1 "2021-12-06T11:08:31Z")

</div>

Hello, I am confused because this basic loop yields a warning:

```julia
i = 5
for j in 1:5
    # ...
    i = "other_value"
    # ...
end

```

```julia
┌ Warning: Assignment to `i` in soft scope is ambiguous because a global variable by the same name exists: `i` will be treated as a new local. Disambiguate by using `local i` to suppress this warning or `global i` to assign to the existing global variable.
└ @ main.jl:4

```

… which I understand. But the following yields no warning:

```julia
for i in 1:5
    # ...
    for j in 1:5
        # ...
        i = "other_value"
        # ...
    end
end

```

… and I’ve just struggle hard to find a bug because I inadvertently wrote something like this X) Is there a reason for the warning to exist in the former case, but not the latter?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 6, 2021, 11:17am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/2 "2021-12-06T11:17:05Z")

</div>

In that case `i` belongs to the scope of the outer loop (thus a local scope).

The problem with the first one is that having a `global i` has a whole set of implications for type inference and performance (because `i` is global), and for that it has to be deal with differently.

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [December 6, 2021, 11:22am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/3 "2021-12-06T11:22:16Z")

</div>

Oh, okay, so there is something really special with the global scope, and this is what this warning is about. It is not just about “i belongs to the outer scope”. I understand now, thank you 🙂

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 6, 2021, 11:26am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/4 "2021-12-06T11:26:18Z")

</div>

Yes, that is why it is highly recommended to avoid the use of global variables. If you wrap everything in a function, or a `let` block, for example, which define their own scope, you won’t get those warnings either.

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [December 6, 2021, 11:32am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/5 "2021-12-06T11:32:16Z")

</div>

Well, in this case I would rather have had the warning instead of my error silently passing X)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 6, 2021, 11:49am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/6 "2021-12-06T11:49:04Z")

</div>

Uhm… not sure if I understand. In that case there would be no “error”, the variable would be modified as intended:

```julia
julia> function f()
           i = 1
           for j in 1:2
               i += 1
           end
           @show i
       end
f (generic function with 1 method)

julia> f()
i = 3
3

julia> let 
           i = 1
           for j in 1:2
               i += 1
           end
           @show i
       end
i = 3
3

```

Take a look at this: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/)

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [December 6, 2021, 11:58am UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/7 "2021-12-06T11:58:26Z")

</div>

Haha yes exactly, but my intent was _not_ to modify it 🙂 Picking `i` for the inner loop variable was an unfortunate name clash in my case. I’ll be more careful in the future ^ ^

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [December 6, 2021, 2:44pm UTC](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/8 "2021-12-06T14:44:01Z")

</div>

You can always declare a variable as `local` to avoid it picking up any previous definitions:

```julia
function f()
   i = 100
   for j in 1:2
       local i = 0
       i = i + j
   end
   println(i)
end

julia> f()
100

```
