# Noob question: why does attached code snippet work on JuliaBox but not JuliaPro?

**URL:** https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714
**Category:** General Usage
**Created:** [January 16, 2019, 6:27pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714 "2019-01-16T18:27:13Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [January 16, 2019, 6:27pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/1 "2019-01-16T18:27:13Z")

</div>

The backdrop: I’m starting to learn the language and am playing around with both JuliaBox and locally installed JuliaPro.

Here’s a silly way to create 3x3 array of three 0s, 1s and 2s arranged randomly:

using Random  
myvec = shuffle([fill(0,3); fill(1,3); fill(2,3)])  
mymat = myvec[1:3]  
for i = 2:3  
mymat = hcat(mymat, myvec[fill(3\*(i-1),3)+(1:3)])  
end

This code snippet works without complaint on JuliaBox running Julia 1.0.3 and fails on JuliaPro running 1.0.2. The error is:

UndefVarError: mymat not defined  
in top-level scope at base/none  
in top-level scope at untitled:5

So it seems that the definition of mymat at line 3 is not available within the for loop in my JuliaPro but is available within the loop when using JuliaBox.

Can someone enlighten me as to what’s going on?

Thanks - and please tell me if this is the wrong place for very basic questions like this.

---

<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 16, 2019, 6:28pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/2 "2019-01-16T18:28:41Z")

</div>

IJulia automatically uses [SoftGlobalScope](https://github.com/stevengj/SoftGlobalScope.jl) whereas plain Julia 1.0 in the REPL does not. It is a very good question.

---

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [January 16, 2019, 8:06pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/3 "2019-01-16T20:06:41Z")

</div>

Thanks for that. So, if I want to use a proper IDE and therefore choose to use JuliaPro locally then must I declare globals explicitly, or can I just add and use the SoftGlobalScope package to make code that works on JuliaBox work w/o modification on JuliaPro?

---

<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 16, 2019, 8:07pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/4 "2019-01-16T20:07:52Z")

</div>

Yes, if you add and use `SoftGlobalScope` then the REPL will work the same as IJulia. There’s also a potential change being worked on for base Julia that would make the `global` annotation unnecessary in such usages.

---

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [January 16, 2019, 8:10pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/5 "2019-01-16T20:10:31Z")

</div>

Ok, tx again.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 16, 2019, 10:07pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/6 "2019-01-16T22:07:27Z")

</div>

> [@StefanKarpinski](#):
>
> Yes, if you add and use `SoftGlobalScope` then the REPL will work the same as IJulia.

No it won’t. To do this, we would need to hook into the REPL, which we aren’t currently doing because it would conflict with other packages:

> <https://github.com/JuliaLang/SoftGlobalScope.jl/issues/2>
>
> As suggested by @dawbarton, in https://github.com/JuliaLang/julia/issues/28789#i…ssuecomment-415063358, it is possible to hack the active REPL to use SoftGlobalScope. Would be nice to add this feature here, so that \`using SoftGlobalScope\` patches the active REPL if there is one.
> 
> Similar \[to this code in OhMyREPL.jl\](https://github.com/KristofferC/OhMyREPL.jl/blob/340bb0e3c63b5f46aee9f14d3f2665465c0a51db/src/OhMyREPL.jl#L79-L110), we would do this in \`\_\_init\_\_\` only in interactive mode.
> 
> @KristofferC, your advice on the cleanest way to patch the REPL's \`eval\` loop would be appreciated.

---

<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 16, 2019, 10:08pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/7 "2019-01-16T22:08:35Z")

</div>

Ah, sorry. I was under the impression that this is what SoftGlobalScope did.

---

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [January 16, 2019, 10:25pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/8 "2019-01-16T22:25:01Z")

</div>

Yup, I tried the three basic SoftGlobalScope calls in JuliaPro already. As Steven said, no such luck… I think I can preface loops with SoftGlobalScope macros but that’s not really any easier than just declaring globals. It’s too bad that JuliaPro and JuliaBox cannot be (easily) made fully interoperable. You guys should highlight that code can’t simply be ported from JB to JP w/o modification. Or perhaps highlight that you’re not kidding about the “Pro” part… Still, channeling (and probably mangling) Aristotle: “The roots of education are bitter, but the fruit is sweet” 😃

---

<div class="post-metadata">

### Author: ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)
#### Post date: [January 16, 2019, 11:17pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/9 "2019-01-16T23:17:45Z")

</div>

The REPL hooks are easy enough to add in with SoftGlobalScope, it’s just a case of running the code in [this gist](https://gist.github.com/dawbarton/0388715fb56fb5cd05e0e4b12c322815). The only problem is that running it blindly (i.e., without considering whether you’re using Revise or not) can mess up Revise.

It should be possible to do something with Requires to automate this but I’ve never got around to it.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 17, 2019, 3:52am UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/10 "2019-01-17T03:52:23Z")

</div>

> [@StefanKarpinski](#):
>
> Ah, sorry. I was under the impression that this is what SoftGlobalScope did.

SoftGlobalScope just provides the transformation tools; it’s up to other packages to use it. Currently only IJulia does AFAIK.

The fact that IJulia does this transformation by default has taken some of the pressure of confused users off, but I still think the REPL needs to do something similar … [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789) still needs to be addressed. There is also the question of whether vscode ([use softscope in terminal? · Issue #540 · julia-vscode/julia-vscode · GitHub](https://github.com/JuliaEditorSupport/julia-vscode/issues/540)) and Juno ([soft global scope · Issue #174 · JunoLab/Juno.jl · GitHub](https://github.com/JunoLab/Juno.jl/issues/174)) should do it.

(I still haven’t seen any cases in which people are confused by the SoftGlobalScope transformation — they are only confused when it _doesn’t_ happen.)

---

<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 17, 2019, 7:30am UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/11 "2019-01-17T07:30:48Z")

</div>

> [@adannenberg](#):
>
> then must I declare globals explicitly

I would recommend wrapping such code (and most code) in functions almost always, without thinking about it. Combined with [Revise.jl](https://github.com/timholy/Revise.jl), it leads to a very convenient and efficient workflow.

Production code in Julia uses globals very sparingly, if at all. Mostly they are used in a pedagogical setting.

---

<div class="post-metadata">

### Author: ![adannenberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adannenberg/32/7869_2.png) [@adannenberg](https://discourse.julialang.org/u/adannenberg)
#### Post date: [January 17, 2019, 12:43pm UTC](https://discourse.julialang.org/t/noob-question-why-does-attached-code-snippet-work-on-juliabox-but-not-juliapro/19714/12 "2019-01-17T12:43:45Z")

</div>

So what’s the best-practice suggestion for iteratively updating a variable? I’m used to writing something like

var2update = initial\_value  
for i=range2iterate  
var2update = some path dependent fn of i and inital\_value  
end

To avoid the bad practice of using globals does the hard scoping force me to wrap every loop in a function? E.g., are you suggesting that I write

loopFn1 = function(var1, numIter)  
for i in 1:numIter  
var1 += 1  
print(var1,“\n”)  
end  
end  
loopFn1(0,5)

instead of

var1=0  
for i=1:5  
var1+=1  
print(var1,“\n”)  
end

I can certainly do this, but it seems cumbersome. I understand how hard scoping helps me avoid hard to diagnose bugs, though, so maybe I should just change my coding habits in other more permissive languages, too.
