# Another possible solution to the global scope debacle

**URL:** <https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894>\
**Category:** Internals & Design\
**Created:** [October 4, 2018, 6:14pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894 "2018-10-04T18:14:07Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 5, 2018, 8:08pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/101 "2018-10-05T20:08:27Z")

</div>

Jeff, you’re a brave man posting this here!

I too think the discussion has run its course. For the technical proposal, a Julep would be great. Whilst your initial description is nice and clear, for most of us (well, at least for me), who are not thinking about scope the whole time, a more elaborate description including examples of 0.6 vs 1.0 vs this proposal would be beneficial.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [October 5, 2018, 8:10pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/102 "2018-10-05T20:10:45Z")

</div>

I think the issue is described as this. There are two competing things we want:

1. We want scoping rules that are easy to explain.
2. We want to make it hard to run into a case where scoping rules actually have to be thought about.

The change which introduced this “debacle” isn’t necessarily bad, it’s just gradient decent to solve (1). After that change, scoping rules were easy to explain. However, now it was quite simple to run into a case where when teaching first time students, you have to explain scoping rules (2).

Jeff’s proposed change is very good on both (1) and (2). Everything outside of a function is global, and so you won’t run into things not being defined outside of the loop.

It does make more variables global “by default”, but I’m not entirely sure that’s something that is a major issue. `@testset` and such tools are supposed to enclose tests into a local scope so that way globals don’t “leak out”, so I think that it’s safe. I wouldn’t worry about the performance aspects either since I think it’s safe to say “if you want performance, make it a function” and small amounts of locality in the REPL won’t do much to the grand scheme of things.

IMO this is a very positive way forward.

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [October 5, 2018, 8:29pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/103 "2018-10-05T20:29:18Z")

</div>

I value the ability to write code in IJulia for example, with as many variables as I feel like, knowing they won’t leak and without using `local` all over the place (or enumerating every variable on top of the `let`)

If I understand this correctly, this would be gone with the proposal. Is there some tweak that could fix it? What if we added `let` to the bag, together with functions, that default to local scope?

Not having a way to mark a piece of code to behave “as if in a function” without wrapping it and running it separately doesn’t feel ideal. One is forced keep track of all the globals at any point, that may be far away, so as not to choose those names and accidentally step over a global.

At this point, as you said, you are convinced there’s no ideal solution. But, we should have easy options to explicity have one behaviour or the other. This proposal, I think, leaves us with only one option.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 5, 2018, 8:31pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/104 "2018-10-05T20:31:08Z")

</div>

> [@Tamas\_Papp](#):
>
> I have been thinking about this rule since I read the post this morning

Bless you for addressing the actual proposal.

> [@Tamas\_Papp](#):
>
> I think it would be a nice solution, and handle the various corner cases in a way that is both
> 
> 1. easy to reason about statically (just from looking at the code),
> 2. does not depend on the variable being defined,
> 3. is a good approximation of what people like to see intuitively.

That was our thinking as well.

> [@Tamas\_Papp](#):
>
> It would be great if someone could prepare a table of various relevant mini-examples, and discuss
> 
> 1. what 0.6 did (optionally, but it would be helpful),
> 2. what happens in 1.0,
> 3. what the rule proposes.

Here are the examples. Julia ≤ 0.6 behavior:

```julia
@assert !isdefined(:x)

for _ = 1:1
    x = "something"
    # creates local `x` because there is no global called `x`
end
# global `x` is still undefined because the above `x` was local
@assert !isdefined(:x)

global x # or assign it a value
@assert !isdefined(:x) # `x` "exists" but is not defined

for _ = 1:1
    x = "something"
    # assigns to global `x` because it "exists"
end
@assert x == "something"

```

Julia 1.0 behavior:

```julia
@assert !@isdefined(x)

for _ = 1:1
    x = "something"
    # always creates a local `x`
end
@assert !@isdefined(x)

x = "something else"
for _ = 1:1
    x = "something"
    # even if a global `x` is defined
    @assert x == "something"
end
@assert x == "something else"

for _ = 1:1
    global x = "something"
    # to assign to global `x` must use `global` keyword
end
@assert x == "something"

```

Proposed behavior:

```julia
@assert !@isdefined(x)

for _ = 1:1
    x = "something"
    # always assigns to global `x`
end
@assert x == "something"

for _ = 1:1
    local x = "something else"
    # to get a local `x` must use `local` keyword
end
@assert x == "something"
# `x` is still "something" because the inner `x` was local

```

In particular, the key is the meaning of this snippet of code:

```julia
for _ = 1:1
    x = "something"
end

```

- In 0.6 the meaning of this code depends on whether a global variable named `x` “exists” or not, which is not quite the same as if it is defined or not—it can exist without being defined (if it is defined, of course, then it must exist). If global `x` does not exist, then it creates local variable `x` inside of the loop. If global `x` does exist, then it assigns to that global `x`.

- In 1.0 the meaning of that code does not depend in any way on what globals exist or are defined: it always defines a new `x` local variable inside the loop body.

- In the proposal, the meaning of the code also does not depend on what globals exist or are defined but in the opposite way: `x` always assigns or updates the `x` global variable.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 5, 2018, 8:35pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/105 "2018-10-05T20:35:26Z")

</div>

> [@Rui](#):
>
> Not having a way to mark a piece of code to behave “as if in a function” without wrapping it and running it separately doesn’t feel ideal.

We didn’t really address `let` explicitly. There’s two ways to go with that:

1. Have the body of `let` behave like a loop body so assignments to variables that are not let-bound or declared `local` will create or update globals.

2. Have the body of `let` behave like a function body so assignments to variables that are not declared `global` will create or update locals.

I think I prefer (2) myself, but @jeff.bezanson had compelling reasons for (1) which I can’t recall at the moment. Perhaps he can refresh my memory.

---

<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:** [October 5, 2018, 8:37pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/106 "2018-10-05T20:37:42Z")

</div>

> [@StefanKarpinski](#):
>
> I think I prefer (2) myself, but @jeff.bezanson had compelling reasons for (1) which I can’t recall at the moment. Perhaps he can refresh my memory.

Currently `for` loops behave like “repeated `let`” — they’re exactly like `let` blocks but with a backwards branch. So continuing to treat them the same feels like fewer special cases to me.

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [October 5, 2018, 8:38pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/107 "2018-10-05T20:38:38Z")

</div>

Maybe one could use `local for`, `local let`, `local begin`. In any case, if the “only functions” thing is not strictly part of the proposal, then I see no problem.

---

<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:** [October 5, 2018, 8:44pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/108 "2018-10-05T20:44:12Z")

</div>

`local for` is kind of a clever idea; that had not occurred to me.

For 1.0, we wanted all forms with scopes to behave the same. Since an assignment inside a function simply must default to creating a local, that implies default-local for everything. So any other approach involves exceptions. I’d prefer to have as few exceptions as possible. I find it easier to say “except functions” than to have to list several things. Functions are also special in that they’re the only way to run code in an order other than top-to-bottom.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 5, 2018, 9:06pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/109 "2018-10-05T21:06:25Z")

</div>

If I may I will bump my question: Will the proposed change handle globals uniformly? Meaning the same rules for the REPL as for modules?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 5, 2018, 9:08pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/110 "2018-10-05T21:08:03Z")

</div>

Yes, that is one of the goals.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 5, 2018, 9:12pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/111 "2018-10-05T21:12:14Z")

</div>

Excellent! And I wouldn’t worry too much about the grumbling concerning the versions and such. Julia quality speaks for itself, so just as long as the change is for the better, I think everyone will just eventually stop complaining and things will be fine.

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [October 5, 2018, 9:23pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/112 "2018-10-05T21:23:36Z")

</div>

Won’t this transition be very annoying? Every single variable outside of a function would require annotations (or at least issue a warning) until about a year from now?

---

<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:** [October 5, 2018, 9:28pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/113 "2018-10-05T21:28:06Z")

</div>

> [@Rui](#):
>
> Every single variable outside of a function would require annotations

Only when they are assigned inside a scope block. Still a lot of variables, but not every one.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 5, 2018, 9:37pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/115 "2018-10-05T21:37:27Z")

</div>

@jeff.bezanson’s proposal indeed seems to me the optimal one if we need to address point (2) by @ChrisRackauckas of not needing to explain anything about scopes when showing this code to a newcomer

```julia
x = 0
for i in 1:2
    x = i
end
x # EDIT: x == 0 in v1.0, but x == 2 with the proposed change

```

I don’t really like it, but given the assumption that we want to solve (2), it does seem the best course of action. But then I would ask, what if I show the newcomer this other code?

```julia
x = 0
f(i) = (x = i)
f(2)
x # EDIT: x == 0 in v1.0, and also x == 0 with the proposed change

```

They will still be puzzled that `x` is not equal to 2, no? And there is _no_ way we should make function scopes leaky to address that!

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [October 5, 2018, 10:40pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/117 "2018-10-05T22:40:13Z")

</div>

Currently:

- I like the “right way” (I will use plenty of `@local` that does `(()->$(esc(code))()` if no alternative is provided but I’d really like one to be provided).
- I worry that the year transition with the forced annotations on each variable in a global block may be worse than the problem it’s trying to solve.

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [October 5, 2018, 10:51pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/118 "2018-10-05T22:51:05Z")

</div>

Allow me to add one character to your first example and you get one of the most common constructs in programming. Something like this is almost guaranteed to come up within your first hour of playing with Julia, WAY sooner than you want to discuss the subtleties of scoping:

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

```

As for the second example …

```julia
x = 0
f(i) = (x = i)
f(2)
x

```

Here the explanation is rather intuitive, even for students coming from a quasi-language like Matlab: “In Julia, variables in functions are local by default. Add the `global` keyword if you want to change a global variable.”

And that’s pretty much all you have to say about scope until you start putting closures inside other functions. Which is a much more appropriate time to have that discussion.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [October 5, 2018, 11:01pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/119 "2018-10-05T23:01:52Z")

</div>

So what is your point exactly? If you find it acceptable to say to the student “In Julia, variables in functions are local by default”, why not just say “In Julia, variables in `function`s, `for` and `let` blocks are local by default"? That’s all it takes to understand v1.0, really, and it makes for a good lesson in coding hygiene…

---

<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:** [October 5, 2018, 11:07pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/120 "2018-10-05T23:07:32Z")

</div>

> [@NiclasMattsson](#):
>
> # EDIT: x == 0 in v1.0, but x == 2 with the proposed change

Actually an error in 1.0, `x == 3` with the proposed change and in \<=v0.6.

---

<div class="post-metadata">

**Author:** ![Rui](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Rui](https://discourse.julialang.org/u/Rui)\
**Post date:** [October 5, 2018, 11:10pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/121 "2018-10-05T23:10:50Z")

</div>

It’s a cost benefit thing.

The more exceptions to the rule, the harder it is to grasp. Functions are an unavoidable exception.

Many more people that never read about scope can get started without bumping into surprises about function scope than about for/let.

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [October 5, 2018, 11:13pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/122 "2018-10-05T23:13:29Z")

</div>

Thanks, I just copied the whole examples without reading the comments. Fixed now. My argument still stands though.

[Previous page](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894.md?page=5)

[Next page](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894.md?page=7)
