-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scoping: blocks allow binding and changing globals without declaring it so #7234
Comments
Concerning your last comment, if I understand it correctly: "And outside the block, I can see foo, so foo is really a global.": |
The |
The challenge is to explain clearly why this happens: julia> begin foo=1; bar() = (foo=foo+1); bar() end
2
julia> begin baz()=(foo=foo+1); baz() end
ERROR: foo not defined
in baz at none:1 |
I think this is a bug! Please relabel this with IMO, begin-end block should have nothing to do with scope: they don't introduce a new scope, nor should they modify the scoping rules of containing blocks. However, in this example the scoping rules for the function
If this is not a bug but a feature, could someone please explain the use case and argue why special-special casing begin-end and if-end blocks is worth the extra language complexity. I plan to update the scope-docs in the near future and resolving this would be helpful. |
Definitely looks like a serious bug to me, both for |
See Jeff's related comment: #10559 (comment) |
Actually this bug might be the evil twin of another "bug":
This is the good twin because people are ok with this behavior. But the problem is that if |
That just seems like a bug to me, not a "good" one, because if begin/end doesn't make a new scope level, local needs to mean a local in the scope test the begin is in. |
Seems like it should behave the same as |
Then we're fine...
But probably both should throw an error on assignment. This is issue #10472. |
In repl when I do this:
I get
ERROR: foo not defined
. So far so good.But if I write it like this:
I get 2.
Wrapping it in block changes the behavior. Apparently the bar() inside a block can access foo without declaring it global. And outside the block, I can see foo, so foo is really a global.
The text was updated successfully, but these errors were encountered: