# 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:** 7

<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:23pm UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/123 "2018-10-05T23:23:11Z")

</div>

Ok, fair enough

---

<div class="post-metadata">

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

</div>

In terms of language design, I think the current scoping rules are fine. In general, local scope should be preferred to global scope. I am **strongly against** making things global by default.

In terms of pedagogy: warnings (or infos) are an ideal solution for this. These user-friendly warnings can be turned off by experienced users who understand scoping rules.

In terms of ease of use: perhaps a convenient syntax to designate variables as global so users don’t have to type out a full `global x`.

EDIT:  
Perhaps my solution is not the best. The important point I’m trying to make is that perhaps the usability concerns can be addressed without resorting to “global by default” behavior.

---

<div class="post-metadata">

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

</div>

> [@jeff.bezanson](#):
>
> > 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.

I thought (2) were coming. I thought `let ... end` was a sugar for `(() -> begin ... end)()`. It was a nice pattern to use in module-level scope to avoid leaking variables as globals. For example, if you need to do some metaprograming using `@eval` and `while`, I suppose simply surrounding the block by `let` would not work anymore with the choice (1)? Also, I suppose top-level for/while loop could be efficiently executed just by surrounding it by `let` in principle with choice (2)?

It looks like both choices have a consistency which is mutually exclusive without 1.0-like scoping behavior. If we are to lose one consistency anyway, why not choose a useful one? Or maybe there are useful cases for choice (1)?

---

<div class="post-metadata">

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

</div>

How about creating new keywords for the proposed changes? E.g. `for!` affects global scope by default, while the behavior for the current (1.0) `for` is maintained.

---

<div class="post-metadata">

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

</div>

To clarify…

```julia
Are you assigning a value to a variable?
   No: this topic is irrelevant
   Yes: Are you assigning within a module?
      Yes: this topic is irrelevant
      No: Are you assigning within a function or struct def?
         Yes: this topic is irrelevant
         No: Are you assigning within a let block?
            Yes: this topic is irrelevant
            No: this topic is relevant

```

As I understand it, this topic applies to top-level assignment within a julia script or the REPL and to assignment within a top-level `for` or `while` loop.

---

<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 6, 2018, 12:02am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/128 "2018-10-06T00:02:51Z")

</div>

As I understand it, this is relevant within modules and in lets. Modules have their globals. And whether let introduces or not a local scope remains to be settled.

---

<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 6, 2018, 12:05am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/129 "2018-10-06T00:05:58Z")

</div>

The way I understand this (see Stefan’s response above), the new rules affect all “global” variables, whether defined at the REPL (module `Main`), or inside a nested module.

---

<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 6, 2018, 12:22am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/130 "2018-10-06T00:22:39Z")

</div>

You know what? This proposal is starting to grow on me. It’s perhaps just a matter of changing my mental model of what constitutes a scope boundary.

Okay, so let’s have modules and functions as the only scope boundaries. But then I think I would prefer `let` blocks to be transparent (leaky) just like `for` (option (1) in @StefanKarpinski’s post). Additionally, I think I would prefer the binding `i` in `for i = ...` to remain _local_, just like in `let i = ...`. There is a nice parallelism there, and it’s easy to remember.

There is also the nice parallelism that’s been discussed that wrapping any top-level `for` into a function doesn’t change the way it works, i.e.

```julia
x = 0
for i = 1:2
    x = i
end
x # x == 2

```

works the same as in

```julia
function f()
    x = 0
    for i = 1:2
        x = i
    end
    x # x == 2
end

```

So, is there any real shortcomings of @jeff.bezanson’s proposal (not just a matter of taste)? It does solve the soft/hard scope complexity that motivated #19324, and I cannot think of any real drawback now… 🤔

(EDIT: … apart from the obvious “how the hell do we get this into 1.x _now_?”)

---

<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 6, 2018, 12:33am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/131 "2018-10-06T00:33:05Z")

</div>

It sounds like you have come around to exactly @jeff.bezanson’s position 😁. I was skeptical about this design initially, but after mulling it over for a while, it’s grown on me. Yes, local is a better default than global, but the more skilled user can easily either (a) do things inside functions or (b) sprinkle a `local` here and there. Putting the burden of knowledge on the more advanced user seems appropriate.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 6, 2018, 12:42am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/132 "2018-10-06T00:42:21Z")

</div>

please see  
[my post asking about this](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/43)  
maybe we are talking about two different things

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [October 6, 2018, 12:44am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/133 "2018-10-06T00:44:48Z")

</div>

The problem I have with this is that functions are supposed to be self contained (and ideally small) units. Therefore it should be easy to tell what values the `for` block is capturing.

If a global `for` block automatically captures global state, a user can easily mess up global state without intending to.

To me it seems better to opt-in to dangerous behavior rather than to opt-out (this is apart from any annoying verbosity).

If working in the REPL has too much friction, I don’t see why we can’t just provide _additional_ features to help mitigate that friction. I don’t deny that working in the REPL is an essential part of most Julia workflows. Perhaps there is a way to be both safe and ergonomic.

---

<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 6, 2018, 12:55am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/134 "2018-10-06T00:55:35Z")

</div>

> [@StefanKarpinski](#):
>
> Putting the burden of knowledge on the more advanced user seems appropriate.

When there’s a choice, Julia generally has people opt into performance instead of opt out (`@inbounds`, `@simd`, `@fastmath`, type-unstable code just works, etc.). Requiring someone to `local` seems to follow that guideline. I think it’s both consistent throughout the language and is more generally just a good design principle.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 6, 2018, 1:09am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/135 "2018-10-06T01:09:24Z")

</div>

So I don’t grok what is it if module contents be global.

---

<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 6, 2018, 1:16am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/136 "2018-10-06T01:16:57Z")

</div>

Yes, this would apply inside of modules—modules are global.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 6, 2018, 1:28am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/137 "2018-10-06T01:28:26Z")

</div>

“modules are global” is different from this?

```julia
module A
a = 5
end
module B
a = 55.5
end
using A
using B
Error both A and B provide a variable named `a` in global scope

```

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [October 6, 2018, 1:31am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/138 "2018-10-06T01:31:32Z")

</div>

> [@StefanKarpinski](#):
>
> Putting the burden of knowledge on the more advanced user seems appropriate.

I would argue that mutating global state is the advanced use case! But I digress. There has been a lot of fruitful discussion and I trust you guys to make the right choice.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [October 6, 2018, 1:31am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/139 "2018-10-06T01:31:49Z")

</div>

Derp - I think I got thrown by this:

> [@jeff.bezanson](#):
>
> the simplest solution to these constraints seems to be just making everything outside of a function global, _unless_ it is explicitly declared with `local` or `let` (or a `for` loop

Thinking that variables outside of functions would all become what @mauro3 called “global global.” But it makes sense from context, apologies for being dense.

---

<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 6, 2018, 1:34am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/140 "2018-10-06T01:34:57Z")

</div>

I think interactive usage is a less advanced use case than scripting and package writing, and in interactive usage, globals are what threads a computation to the next.

---

<div class="post-metadata">

**Author:** ![00vareladavid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00vareladavid/32/23521_2.png) [@00vareladavid](https://discourse.julialang.org/u/00vareladavid)\
**Post date:** [October 6, 2018, 1:41am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/141 "2018-10-06T01:41:50Z")

</div>

I’m convinced! I guess I was focused too much on package writing rather than interactive use.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 6, 2018, 2:03am UTC](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894/142 "2018-10-06T02:03:32Z")

</div>

What we do should help and not hinder future realization of namespaces, api protocols, trait organization, …  
[I’m not implying anything about the proprosal]

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

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