I still think the solution I proposed would work in this case (which is discussed in the 1.5 blog):
Despite the “on paper” simplicity of the 1.0 scope rule, many people found it unintuitive and annoying. Consider the following example:
s = 0
for i = 1:10
s = s + i
One would naively guess that this prints the number 55 , and prior to Julia 1.0 that’s exactly what it did. But in 1.0, we consistently made for loops introduce new local scopes, which means that this code would throw an undefined variable error because s is deemed to be local to the loop: since there is no pre-existing local s (only a global s ) and s is assigned in the loop, it is local and the first iteration of the loop tries to access an undefined local s .
My proposed solution was to check if there was a “read” of a variable before there was a “write” to it in the loop. In this case there is: s + i “reads” the value of s. Then, that would mean that the variable must have existed prior to entering this loop, and s would have become global inside the loop.
So the simple rule “if you assign to a variable in a local scope (function or loop) and there’s no local by that name, then that assignment creates a new local variable” would work just fine in a global scope, if supplemented with “was there a read first?”.
It was discussed and for a while it was my preferred possible fix. However, a number of legitimate potential issues were raised in that thread and elsewhere (it’s been discussed so much it’s hard to keep track). I won’t repeat them here.
Jeff prototyped it and we played around with it for a while to get a feel for the behavior. It was in fact kind of fussy and brittle. When you account for all the possibilities, the rule is not so simple to explain or to implement (that patch adds an enormous amount of Scheme code to the parser). It also fails to address the fact that when someone assigns to a variable in a loop that is already a global, it’s actually ambiguous what they mean: did they intend to clobber the global, or did they want a local that shadows the global? Yes, we could make it work in some cases like this example, but that wouldn’t address the deeper ambiguity. So after a lot of discussion, Jeff and I decided against it. It just didn’t feel right.
We came up with the current solution as an alternative and some time later prototyped it. In the REPL it uses a proven heuristic from 0.6 and just does the convenient thing. In a file, on the other hand, it forces you to disambiguate in exactly the case where the heuristic is necessary and where the problematic ambiguity exists. This one “feels right”: it’s relatively simple and it prevents people from accidentally writing broken and/or confusing code. In PkgEval runs (with the warning made fatal) we found that it hardly ever triggered a warning in package code, and when it did, that code was usually actually buggy.
I’m also very done with conversations on this subject unless they are “This works so well, thank you very much!”
This is just not nearly as simple as it sounds. “Does this image contain a cat?” also sounds like a simple question. First in time or in space? Does the read have to actually happen, or just be possible in some way? How does it combine with gotos, macros, and closures? What if there are no reads? If there are no reads, you probably meant to assign the outer variable? This approach does handle some simple examples well, but as soon as code gets a bit more complex, good luck. I just don’t agree that “does a variable of that name already exist?” is harder to explain than dataflow analysis.
Right, so why not just directly consult whether the variable already exists, instead of trying to infer that from a subtle property of code that uses it?
I had professors who would handle similar situations by asking that the student reviews the relevant discussions, compiles a compendium of code snippets that were used to illustrate various trade-offs, with a table summarizing how each scope proposal would work in each situation. By Monday
I object strongly to the reflexive attitude of superior insight into design decisions. Such criticisms are cheap and amount to a tax on those who do the hard work of building something that people care about. Regardless of the merits of the student’s criticism, I would favor guiding them in a direction of greater self-skepticism.
This will not change. This is the backside of every great success and achievement, especially for complex ones. Try to think of this not as complains. It is an expression of great involvment for example, or what ever fits for you. Frustration will let you overlook the vast amount of triumph Julia is (admitted a bit of exaggeration )!
On the other side I don’t see OP as dismissive. Not at all. It isn’t skepticism or criticism. All negatively annotated. It is just only another contribution to a public debate of a specific detail. This is science and development, it is never finalized.
All true, but this is setting up a false comparison: The non-REPL julia rules vs. a language without loops introducing scopes.
It is now 2 years since IJulia patched things to add the v0.6 scoping rules back in. How many issues have you seen in discourse/stack exchange/etc. on Jupyter scoping rules since then. I have seen zero. Literally none. I may have missed something, but it might actually be zero. Anytime jupyter it is mentioned in a quetsion/comment it is always the “Jupyter does what I expected. I don’t understand why running it as a .jl file doesn’t behave like jupyter”.
That doesn’t mean it is possible to immediately switch everything over to jupyter/1.5 REPL rules (as it is breaking behavior and there were legitimate tradeoffs which led to the decision for v1.0), but it suggests there was a previous design does have a coherent set of rules given the various tradeoffs involved.
I see, it wasn’t clear.
Looking back it was the quote of the students:
which was called dismissive I guess.
Yes it is, but they are strudents in a (probably) beginners course, and I guess, it was just said without so much thinking. It should not be taken serious by the words.
But OP point still is valid as a general point of view. And it should be explicit said, that this type of topic (OP) is always welcome, now and in future, because Julia must always develop further. There should never be a “it is now what it is forever so be quiet”.
To be clear: it wasn’t said, that OP should not be created. I just felt some bad tendencies, first when “dismissive” wasn’t clearly pointed to the quote “ugly”, second when I felt some kind of frustration with the topic in general.
It was mainly a comment on the previous post, with just a side note on the student’s remark. I meant to reflect on the sometimes thin line between teaching skepticism and avoid being overly dismissive.