# Global scope confusion again

**URL:** <https://discourse.julialang.org/t/global-scope-confusion-again/20044>\
**Category:** General Usage\
**Tags:** question, scope\
**Created:** [January 24, 2019, 5:08pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044 "2019-01-24T17:08:01Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [January 25, 2019, 5:48pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/21 "2019-01-25T17:48:22Z")

</div>

> [@jlperla](#):
>
> he thinks that the new scoping rules are sufficiently perplexing

I think the immediate issue is unrelated (or very weakly related) to scoping — it is simply a pre-existing variable `g`. Eg

```julia
julia> g = 1
1

julia> g() = 1
ERROR: cannot define function g; it already has a value
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

---

<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, 5:55pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/22 "2019-01-25T17:55:38Z")

</div>

Maybe the error message could be improved… something like: `"cannot define function g: "g" is already used as a variable name.`

---

<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:** [January 25, 2019, 5:58pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/23 "2019-01-25T17:58:33Z")

</div>

PRs welcome if someone has a better wording, although the current wording seems fairly clear already.

---

<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, 6:05pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/24 "2019-01-25T18:05:36Z")

</div>

One could read the “it” in the message as “function g”. Then the error will read “function g already has a value” and one might understand from that that you cannot redefine “function g”.  
I’d make a PR if I someone could point me in which file the error message is written.

---

<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:** [January 25, 2019, 6:07pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/25 "2019-01-25T18:07:12Z")

</div>

> [@favba](#):
>
> Maybe the error message could be improved…

I think the issue here is that the error message and the subsequent suggestions/questions on this forum were just completely ignored.

---

<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:** [January 25, 2019, 6:26pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/26 "2019-01-25T18:26:27Z")

</div>

Let’s make the most of it and see if we can’t get a better error message out of the experience.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [January 25, 2019, 6:40pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/27 "2019-01-25T18:40:06Z")

</div>

> [@Tamas\_Papp](#):
>
> I think the immediate issue is unrelated (or very weakly related) to scoping — it is simply a pre-existing variable `g`

Yup. To me this is an example of noise being generated by the confusion around scoping, which leads people to miss a more obvious problem. I think the error message is otherwise clear if people stop worrying about scope issues, like they were able to before.

---

<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, 7:42pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/29 "2019-01-25T19:42:48Z")

</div>

> [@jlperla](#):
>
> If I were to guess the broader point: the mental model of scoping he has in mind (that I can take almost any code and put a function around it, like you could do in v0.6) is broken, and he thinks that the new scoping rules are sufficiently perplexing that it is a signal of what else is counter-intuitive and possibly “buggy” in the language.

Yes. That’s exactly right. My subjective global perception concerning Julia suddenly changed on the account of this scoping " tweak." In my mind, it is now a product by programmers for programmers. Within that revised mental model concerning the product, Stefan is correct in stating that my reaction to the scoping rule is an over-reaction.

There was a time in history when one had to be either mechanically inclined, rich and probably both to operate an automobile. My time and cognitive bandwidth are sufficiently limited that it does not make sense for me to operate Julia. Especially, because I imagine that there will probably be more unduly taxing surprises down the road.

---

<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:** [January 25, 2019, 7:48pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/30 "2019-01-25T19:48:29Z")

</div>

Don’t give up. It seems that there is just a simple small missunderstanding which bothers you. If you try to overcome it now you will be rewarded with the best programming environment currently available (my opinion).  
I would be more specific but it is not so clear to me what exactly is the problem.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [January 25, 2019, 7:54pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/31 "2019-01-25T19:54:43Z")

</div>

> [@Paul](#):
>
> In my mind, it is now a product by programmers for programmers.

He summarized his issue elegantly. While I think that it is largely inaccurate, I have to admit that the scoping issues and the inability to solidify a solution are not a good signal. In my mind, another bad signal is that whenever anyone mentions debuggers on these forums they get attacked with “debuggers are not necessary if you write code properly… i have written 100,000 lines of code without them!” responses.

Luckily, the broader signal we have on both of these is: (1) the scoping issue will be resolved, but they want to make sure they do it right; and (2) lots of people want a Matlab-style debugger, but the funding isn’t there yet and it isn’t something the “power users” have incentives to do on their own.

So, with all of that. I also implore you not to give you, as there are other signals you may have missed.

---

<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:** [January 25, 2019, 8:06pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/32 "2019-01-25T20:06:09Z")

</div>

> [@jlperla](#):
>
> whenever anyone mentions debuggers on these forums they get attacked

I would say that this is a misrepresentation, especially in light of recent developments like

> <https://github.com/JuliaDebug/ASTInterpreter2.jl/pull/37>
>
> This is a pretty big overhaul of this package. I've tried to keep its original f…unctionality, but perhaps a split should be the next step (see #32). The first 5 commits get the tests passing, with the exception of the ui tests which I didn't even look at (CC @staticfloat re #34, #36; @pfitzseb re #33).
> 
> The rest is much more ambitious. I worked a lot on the performance, since an "easy" way to add robust breakpoints, etc, is via running your code in the interpreter. And of course there's interest in using the interpreter to circumvent compile-time cost. But then performance matters.
> 
> The most important changes here are focused on reducing the cost of dynamic dispatch (or its equivalent here, "dynamic lowered code lookup"). Some highlights:
> \- At the lowest level, all calls are to Builtins and IntrinsicFunctions. I used the \`tfunc\` data in \`Core.Inference\` to auto-generate an evaluator that resolve all calls with fixed number of arguments. A straightforward extension would be resolve all calls with bounded numbers of arguments (e.g., those that have between 2 and 4 arguments).
> \- To reduce the overhead of \`which\`, this adds "local method tables," one per :call Expr. These are cached by exact type, since \`isa(x, Int)\` is fast but \`isa(x, Integer)\` is slow. So it uses \`MethodInstance\` comparisons rather than \`Method\` signature comparisons, even though it might look up the same lowered code. I think a slightly more elegant way to do this would be to add a new type,
> 
> \`\`\`julia
> struct LocalMethodTable
> call\_expr::Expr
> knownmethods::TypeMapEntry
> end
> \`\`\`
> 
> to the list of valid types in a \`CodeInfo\`. (I did it this way at first but it breaks things like basic-block computation and ssa-usage analysis. So I resorted to storing this info in a separate fields.)
> 
> For a simple summation test, I'm getting about 15us per iteration. Compiled code is about 5ns, so this is still dirt-slow. But just getting it to this point was quite a major overhaul.
> 
> CC @JeffBezanson, @vtjnash, @StefanKarpinski.

Also, while I recognize that you want a debugger, I don’t think that turning this thread into yet another debugger discussion is a good idea.

---

<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:** [January 25, 2019, 8:24pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/33 "2019-01-25T20:24:33Z")

</div>

> [@favba](#):
>
> I’d make a PR if I someone could point me in which file the error message is written.

I think it is

> <https://github.com/JuliaLang/julia/blob/341587592daccd144f5653c86cc1e362fe209838/src/method.c#L646>

---

<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, 8:26pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/34 "2019-01-25T20:26:12Z")

</div>

> [@jlperla](#):
>
> the inability to copy/paste code, reorganize in functions, etc. without thinking through scoping rules

In my opinion, not thinking about scoping rules lead tons of beginners to test julia interactively, use global variables inside loops, and come to the conclusion that julia is slow.  
I’m of the opinion that the new behaviour is better and lead to better habits.

It can be mildly (and I mean really really mildly) annoying for interactive usage.  
I also think that `SoftScopeRule.jl` is already the solution to this problem. The only thing missing is a REPL mode that uses it, for those who want to teach or can’t bare writing `global` in some places.

I think most of the users are fine with the change. It’s just that those who don’t like this change are very noisy.

---

<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, 8:28pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/35 "2019-01-25T20:28:02Z")

</div>

> [@jlperla](#):
>
> So, with all of that. I also implore you not to give you, as there are other signals you may have missed.

> [@oheil](#):
>
> Don’t give up.

Thank you for your encouragement. I’ve been rooting for Julia to succeed for so many years. I’ll give it another try once I get my hands on a good Julia programming book based on Julia 1.x. Gotta give Mathematica a try, but that’s another story.

---

<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:** [January 25, 2019, 10:35pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/36 "2019-01-25T22:35:24Z")

</div>

I am sure that with a distance of a few days this will seem just a minor issue. I wouldn’t give up on Julia if I were you: it is a very clean and understandable programming language. There will be fewer and fewer surprises as you go.

---

<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 26, 2019, 2:41am UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/37 "2019-01-26T02:41:13Z")

</div>

Thanks Petr. I really appreciate your encouragement!

Cheers

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 26, 2019, 7:41am UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/38 "2019-01-26T07:41:35Z")

</div>

> [@favba](#):
>
> In my opinion, not thinking about scoping rules lead tons of beginners to test julia interactively, use global variables inside loops, and come to the conclusion that julia is slow.

If this is what is intended, wouldn’t be more simple and clean to deprecate `for` in global scope?

---

<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:** [January 26, 2019, 8:54am UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/39 "2019-01-26T08:54:37Z")

</div>

Not sure if you are being serious, but `for` is used extensively in global scope, eg for [definitions that follow a template](https://github.com/JuliaLang/julia/blob/77739c878007e874e86e8324598c3de11c952c6a/base/float.jl#L57-L70). The confusion is not about `for` per se.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 26, 2019, 9:35am UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/40 "2019-01-26T09:35:58Z")

</div>

I think he’s not actually suggesting to deprecate `for` in global scope but instead pointing out (and I agree on this) that the point made by @favba isn’t a good one as it seems to suggest (maybe unintentionally) that we should make it as hard as possible for users to use Julia interactively as they will never get the speed they would get in local scopes and thus wrongly conclude that Julia is slow.

In my opinion, having an intuitive (somewhat slower) interactive mode is just as important as being able to get C-like speed in well-written functions. One thing (among many others) that made me fall in love with Julia was that I can work on any mental level (e.g. interactive exploration, somewhat performant coding, beast mode!), always choosing the one which is most appropriate for my task. We should try to avoid improving one level at the cost of another (which is a difficult task, of course).

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 26, 2019, 12:42pm UTC](https://discourse.julialang.org/t/global-scope-confusion-again/20044/41 "2019-01-26T12:42:10Z")

</div>

> [@StefanKarpinski](#):
>
> The rule is very simple: if you want to modify a global variable inside of any scope-creating construct (loops, functions, let) you need to use the `global` keyword. Without that, an assignment inside any scope-creating construct assigns to a local variable if one already exists, or declares a new one if none exists;

Just a quick question, how many julia users complained, that loops didn’t create a new scope in other languages? What problems, for me as a programmer, are solved by creating a new scope at loops?

While i clearly understand scope boundaries at functions/calls or let blocks, i (for myself) still wonder, what loops have to do with scope. A loop is a repetition - the numbers known at the point of entering the loop, or just checking a condition and branch back to a previous point in the sequence of execution - but although we all tend to indent the loop content in code, was for me in most of my programming experience still the same scope; usually the scope of ‘this’ function.

[Previous page](https://discourse.julialang.org/t/global-scope-confusion-again/20044.md?page=1)

[Next page](https://discourse.julialang.org/t/global-scope-confusion-again/20044.md?page=3)
