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

local scope in Julia 1.0 #28750

Closed
rsturley opened this issue Aug 19, 2018 · 8 comments
Closed

local scope in Julia 1.0 #28750

rsturley opened this issue Aug 19, 2018 · 8 comments

Comments

@rsturley
Copy link

The local scope in while doesn't seem to work as documented. The following code in version 0.6.4 runs without error.

i=0
while i<3
   i += 1
end

The same code running in Julia 1.0.0 gives an error indicating the the i inside the while loop is not defined.

julia> i=0
0

julia> while i<3
       i += 1
       end
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at .\REPL[2]:2 [inlined]
 [2] top-level scope at .\none:0

julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: AMD A10-6700 APU with Radeon(tm) HD Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, bdver2)

julia>

Unless I've missed a change in the current version, I would expect the global variable i to be accessible inside the while loop. The same behavior also happens inside a for loop or a function.

The documentation for Version 1.0 says the following:

"A new local scope is introduced by most code blocks (see above table for a complete list). A local scope inherits all the variables from a parent local scope, both for reading and writing. Additionally, the local scope inherits all globals that are assigned to in its parent global scope block (if it is surrounded by a global if or begin scope). Unlike global scopes, local scopes are not namespaces, thus variables in an inner scope cannot be retrieved from the parent scope through some sort of qualified access."

If the new behavior was intentional, then the documentation should be changed to match. My vote would be to change the behavior to match the documentation, which seems like a more sensible approach to me.

@yuyichao
Copy link
Contributor

The behavior does not contradict the document. Please keep reading for more detailed explaination in the examples below (search for "this is also the default"). Dup of #28523

@yuyichao
Copy link
Contributor

And also more explicitly

Thus global variables are only inherited for reading but not for writing:

The doc is correct and the behavior (change) is intended so there's nothing to fix here (hence the close). If you think the doc could be improved in some way (the language used, the order of examples/rules) you are of course welcome to submit a PR to change that.

@ghost
Copy link

ghost commented Oct 19, 2018

Thus global variables are only inherited for reading but not for writing

Then why the code below still works inside notebook? I guess it's no longer a "global" scope?

  i=0; while i<3
      i += 1
  end

@StefanKarpinski
Copy link
Member

#28789 (which is the link right above your question).

@ghost
Copy link

ghost commented Oct 19, 2018

But that link does not explain my question.
This person
#28789 (comment)
just mentions that behaviour in Repl and Jupiter will be different, but not why.
I find it confusing to have code working in different ways in these 2 media.
I think both of them should be just as easy for interactive use/exploration.

@KristofferC
Copy link
Member

@ghost
Copy link

ghost commented Oct 19, 2018

And why the Repl does not?

Is there a way to make it work like that by default, as Jupiter?

@KristofferC
Copy link
Member

It might in the future (#28789). Things have to move slower here than in packages though. I suggest further discussion goes to https://discourse.julialang.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants