Skip to content
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

Closed
tonyhffong opened this issue Jun 12, 2014 · 10 comments
Closed
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@tonyhffong
Copy link

In repl when I do this:

foo = 1
bar() = (foo=foo+1)
bar()

I get ERROR: foo not defined. So far so good.

But if I write it like this:

begin
  foo = 1
  bar() = (foo=foo+1)
  bar()
end

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.

@mauro3
Copy link
Contributor

mauro3 commented Jun 16, 2014

Concerning your last comment, if I understand it correctly: "And outside the block, I can see foo, so foo is really a global.": begin-end blocks do not introduce a new scope, thus this is expected.

@ihnorton ihnorton added bug and removed bug labels Jul 2, 2014
@ViralBShah
Copy link
Member

The begin-end blocks not introducing new scope is already documented. Not sure what further to do here.

@jiahao
Copy link
Member

jiahao commented Jan 2, 2015

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

@mauro3
Copy link
Contributor

mauro3 commented Jun 15, 2015

I think this is a bug! Please relabel this with bug and remove the doc label.

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 bar changes from hard-scope to semi-soft-scope (see #10559). Note that the same applies to if-blocks:

julia> if true; foo=1; bar() = (foo=foo+1); bar() end
2

julia> if true; bar() = (foo=foo+1); bar() end
ERROR: foo not defined
 in bar at none:1

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.

@ScottPJones
Copy link
Contributor

Definitely looks like a serious bug to me, both for if and begin/end.

@JeffBezanson JeffBezanson self-assigned this Jun 15, 2015
@JeffBezanson JeffBezanson added bug Indicates an unexpected problem or unintended behavior and removed docs This change adds or pertains to documentation labels Jun 15, 2015
@mauro3
Copy link
Contributor

mauro3 commented Jun 15, 2015

See Jeff's related comment: #10559 (comment)

@JeffBezanson
Copy link
Member

Actually this bug might be the evil twin of another "bug":

julia> begin
         local x = 1
       end
1

julia> x
ERROR: UndefVarError: x not defined

This is the good twin because people are ok with this behavior. But the problem is that if begin doesn't introduce a new scope, what is the local x local to? We might want to disallow this.

@ScottPJones
Copy link
Contributor

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.

@ssfrr
Copy link
Contributor

ssfrr commented Jun 16, 2015

if begin doesn't introduce a new scope, what is the local x local to?

Seems like it should behave the same as local x = 1 at global scope, unless there's something I'm missing.

@mauro3
Copy link
Contributor

mauro3 commented Jun 16, 2015

Then we're fine...

julia> local x=1
1

julia> x
ERROR: UndefVarError: x not defined

But probably both should throw an error on assignment. This is issue #10472.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

8 participants