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

Problems with local scope #11696

Closed
krychu opened this issue Jun 12, 2015 · 7 comments
Closed

Problems with local scope #11696

krychu opened this issue Jun 12, 2015 · 7 comments
Labels
docs This change adds or pertains to documentation

Comments

@krychu
Copy link

krychu commented Jun 12, 2015

Hello,

Consider two examples:

x = 1
for i = 1:10
    x = 10
end
println( x )

out: 10
x = 1
function a()
    x = 10
end
a()
println( x )

out: 1

According to the docs both the for loop and the function definition should introduce a new scope with an inner variable x. But this doesn't seem to be the case for the for loop. I'm sure I'm missing something (?)

Here's the relevant snippet from the docs:

"As a consequence, the only way to assign to a global variable inside a non-top-level scope is to explicitly declare the variable as global within some scope, since otherwise the assignment would introduce a new local rather than assigning to the global."

Mac OS, Julia 0.3.9

@yuyichao
Copy link
Contributor

This is the documented correct behavior.

See http://julia.readthedocs.org/en/release-0.3/manual/variables-and-scoping/

In particular, the following text and the examples below it.

An assignment x = y introduces a new local variable x only if x is neither declared global nor introduced as local by any enclosing scope before or after the current line of code.

@yuyichao
Copy link
Contributor

Actually when I read the document on the variable scope just now, it isn't very clear on what's the difference between a let block and a function in terms of scope. Maybe it can be improved?

@krychu also note that you didn't call the function in your second example (although it doesn't make a difference)

@yuyichao yuyichao reopened this Jun 12, 2015
@yuyichao yuyichao added the docs This change adds or pertains to documentation label Jun 12, 2015
@krychu
Copy link
Author

krychu commented Jun 13, 2015

Sorry I forgot to add a() above (I've added it now). I've read the docs multiple times and it is not clear. Both for and function are listed in the docs as constructs introducing new scopes. So all further scope rules described in the docs should apply to them equally.

The docs say:

An assignment x = y introduces a new local variable x only if x is neither declared global nor introduced as local by any enclosing scope before or after the current line of code.

which would suggest for and function will inherit the outer x.

Then the docs say:

When a variable is introduced into a scope, it is also inherited by all inner scopes unless one of those inner scopes explicitly overrides it.

and this:

... the only way to assign to a global variable inside a non-top-level scope is to explicitly declare the variable as global within some scope, since otherwise the assignment would introduce a new local rather than assigning to the global.

which would suggest that for and function will introduce a new local variable x.

@krychu
Copy link
Author

krychu commented Jun 13, 2015

Also this part is confusing

[...] Due to the special identification of the prompt’s scope block with the global scope, it is not necessary to declare global y inside the loop. However, in code not entered into the interactive prompt this declaration would be necessary in order to modify a global variable.

which doesn't seem to be true as this script (run from outside the interactive prompt):

y = 0
for i = 1:1; y = 10; end
println( y )

modifies the global y without using global declaration within the loop.

I'm pointing these out so that the docs can be made clearer for newcomers (like me).

@mauro3
Copy link
Contributor

mauro3 commented Jun 13, 2015

This is soft vs hard scope of functions which is not described in the docs, its issue is #9955. And also have a look at #10559. And if anyone improves the scope docs, the meaning of global should be defined: i.e. that a global is a module-wide global. There are no truly global globals (which is different from Matlab).

@krychu
Copy link
Author

krychu commented Jun 15, 2015

Thanks!

@JeffBezanson
Copy link
Member

Closing as dup of #9955.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

4 participants