REPL and for loops (scope behavior change)


And it begins: :sweat_smile:


I’m hosting a hands-on introduction to Julia for PhD-students in energy systems modeling. The whole mini-course will have three 2.5-hour sessions, and I just came back from the first session an hour ago. Four out of 12 students were stumped by this issue completely independently of each other. All four worked interactively in the REPL to make their code work before saving it to a file. None of these four had enough programming experience to know what scoping meant, but all are capable of hacking together Matlab scripts.

The bottom line is this. Programming newbies who try everything out in the global scope of the REPL find this scoping behavior completely counter-intuitive. This makes the language seem unfriendly, bordering on broken in this particular case.

As for me, I think scoping rules are an intermediate level topic and shouldn’t be necessary to bring up in an introductory-level course. If you just tell students to wrap all code in functions, even in the REPL, then suddenly the language seems quite a bit less interactive and harder to work with.

I understand why the scoping changes were made in 1.0, but I hope that automatic let-block-wrapping idea that Stefan mentioned gets implemented and released ASAP. It may even be worth making a 1.01 bug fix release just for that. Because I’m pretty sure I lost a third of my potential new Julia recruits just because of this.


You really cannot win. We used to get so much flack for the scope rules being slightly complicated specifically to make this case work in an intuitive way. So we fix that and now we have this. :pensive: In any case, I think that the current behavior, though less convenient, is the right one since it is the one that is statically resolvable. We should experiment with the automatic let wrapping in the REPL, however.


Can that (let wrapping) be done in 1.0.1, instead of waiting for 1.1?
It is only the REPL :wink:

I am a big fan of the new behavior, the old “spooky action at a distance” scoping rules was one of the biggest warts on a beautiful language, thanks for taking the hard decision to fix this in v1.0.0…

1 Like

Yes, this does sound very interesting. It could help with closures from the REPL too.

1 Like

Here is a Github issue if you’d like to follow along there.


This post was flagged by the community and is temporarily hidden.


Wondering if there is any update on this.

1- there were some very long discussions here and on github. Does anybody know if any conclusion has been reached?

2 - I was wondering if the global declaration and the explicit reference creates any (significant) overhead considering that it’s looped over at every iteration?

3 - if a solution is still open for discussion, maybe something inspired by PHP’s lambdas syntax would work? In julia it could look like:

somevar, someothervar = 10, 100

for i in 1:1_000 accessing (somevar, someothervar)
 # do stuff with somevar and someothervar

The accessing block would be optional. It could start as syntactic sugar for the ugly global as it’s more readable, in my opinion. Plus, as a less imperative approach, later on, the underlying implementation could be optimised at compiler level.

Just more 2c.


Dear Stefan:

I can no longer even make the most basic iterations work switching from Julia 0.x to 1.x - probably because I don’t understand the computer science behind the “tweak.”

You say that this is a mere " inconvenience." To the beginner, this is inconvenient in the same way as a broken condom is inconvenient in regards to preventing pregnancy.

I am shattered. With this " tweak", my love affair with Julia (0.x) has suddenly turned into a nightmare. I am back to trying to turn me into a computer scientist, with Julia (1.x)? It’s not going to happen.

I was doing straightforward scientific programming Julia 0.x with no issues, ever. Now, it seems that I’ve run into a concrete wall with this Julia 1.x. " issue."

Please tell me that I can get over this in a day or two by pointing me to THE helpful resource that will help make me whole again. Save me from the truly inconvenient jaws of Mathematica.


What seems to be the problem?


Just use IJulia. It will have the same “feel” as v0.6.
Also, it wouldn’t hurt just trying to learn the new scoping rule…


Or more generally, use Put it in your startup file if you wish.


IJulia does not give me the speed that I need for my calculations. I need a local installation with a lot of help from the GPU. That’s why I ended up going from IJulia (0.x) notebooks on the web to the desktop. That’s how I landed with JuliaPro 1.x

I got some help with scoping a simple loop this morning. Immediate success. I was happy. Then I added another loop to the program. The same recipe does not work. I’d have to go into computer science mode to get over the hump. That hurts a lot. I don’t think like a programmer and I don’t want to learn how to think like a programmer. I’d rather be waterboarded, right now.


Thanks for this. I’ll check it out. If SoftGlobalScope.jl does the job, I’ll be back and we’ll have a party. I have to say that in the meantime, I’ve already got a body part or two in the jaws of Mathematica. Wish me luck.


Thanks for the interest. My original problem lives here: Global Scope Confusion


You can run IJulia locally. Don’t confuse IJulia with juliabox or whatever cloud server you are using.


SoftGlobalScope doesn’t work in the REPL. It only works in environments that choose to use it, which so far is just IJulia.


Ah, thanks for the correction. I had thought it did.


Aahhh. That sounds promising. Thanks.:grinning:


This is slightly off topic, but I never liked much having jupyter notebooks in the middle of my browsing tabs.
I like nteract, which provides an isolated desktop app for jupyter notebooks.
It is beta, though, so you can expect some bugs lurking around.
In Unix plataforms it is able to “self-setup” it to look for installed jupyter kernels. I don’t know if it work as easily on Windows.

1 Like