Global scope confusion again

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

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
1 Like

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

2 Likes

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

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.

1 Like

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

1 Like

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

4 Likes

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.

1 Like

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.

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.

1 Like

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.

2 Likes

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

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.

1 Like

I think it is

1 Like

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.

4 Likes

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.

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.

1 Like

Thanks Petr. I really appreciate your encouragement!

Cheers

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

Not sure if you are being serious, but for is used extensively in global scope, eg for definitions that follow a template. The confusion is not about for per se.

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).

7 Likes

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.

3 Likes