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

global keyword in inner scopes #7264

Closed
amckinlay opened this issue Jun 14, 2014 · 7 comments
Closed

global keyword in inner scopes #7264

amckinlay opened this issue Jun 14, 2014 · 7 comments
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@amckinlay
Copy link
Contributor

global does not always refer to the global scope.

In [1]: for i = 1:1
   ...:     local z = 1
   ...:     for i = 1:1
   ...:         global z = 1
   ...:     end
   ...: end

In [2]: z


z not defined
at In[2]:1

Another example:

In [17]: function foo(z)
    ...:     global z = 2
    ...: end
Out[17]: foo (generic function with 1 method)

In [18]: foo(3)
Out[18]: 2
In [19]: z

z not defined
at In[19]:1

Version information:

In [3]: versioninfo()
Julia Version 0.2.0

Commit 05c6461 (2013-11-16 23:44 UTC)
Platform Info:
  System: Windows (x86_64-w64-mingw32)
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
  LAPACK: libopenblas
  LIBM: libopenlibm

Note: I believe this same behavior occurs on the latest snapshot.

@elextr
Copy link

elextr commented Jun 15, 2014

This appears to be a symptom of the REPL having to use a "fake" global scope for code typed into it.

Both examples work fine from files.

The manual obliquely refers to the fact that in the REPL global has slightly different semantics.

@amckinlay
Copy link
Contributor Author

@elextr All the manual says is that global scope in the REPL is inherited with local variable semantics into inner scopes. So, you can assign to a global from an inner scope without having to use the global keyword.

Edit: Sorry, this comment is a little confusing. What I should have said is that it is inherited "like" a local variable, but is still global and should be accessible via the global keyword no matter what inner scope you're in.

@elextr
Copy link

elextr commented Jun 16, 2014

The part of the manual I was referring to is in http://docs.julialang.org/en/release-0.2/manual/variables-and-scoping/

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

This implies that at the REPL the "global" is not the true global scope. Indeed having the REPL use the true global scope would be dangerous, a user typing at the REPL could make a (un)lucky choice of name and affect the REPL or Julia itself.

But it seems that Julia doesn't hide the differing semantics well enough.

@amckinlay
Copy link
Contributor Author

So, the global keyword should refer to the enclosing scope even if the
enclosing scope is not global? That doesn't make sense.
On Jun 15, 2014 10:19 PM, "elextr" notifications@github.com wrote:

The part of the manual I was referring to is in
http://docs.julialang.org/en/release-0.2/manual/variables-and-scoping/

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

This implies that at the REPL the "global" is not the true global scope.
Indeed having the REPL use the true global scope would be dangerous, a user
typing at the REPL could make a (un)lucky choice of name and affect the
REPL or Julia itself.

But it seems that Julia doesn't hide the differing semantics well enough.


Reply to this email directly or view it on GitHub
#7264 (comment).

@elextr
Copy link

elextr commented Jun 16, 2014

In the REPL, global refers to a hidden local scope that "surrounds everything you type". It is indeed not the global scope. As the referred manual section notes, its semantics may therefore be slightly different.

In 0.3 a lot of the REPL has been changed, but I don't know if this has been fixed.

@JeffBezanson
Copy link
Sponsor Member

No, in the REPL global variables are the same as any other global variables. If I type x=1 then Main.x exists, like any other global. It's just that the REPL doesn't always require the global keyword for assigning to globals; in the REPL, globals can be automatically inherited like locals from an enclosing scope, but they are still real globals.

@amckinlay
Copy link
Contributor Author

@JeffBezanson So, this issue is a bug.

@JeffBezanson JeffBezanson changed the title global keyword broken global keyword in inner scopes Jun 17, 2014
ihnorton added a commit that referenced this issue Jul 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants