-
-
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
Unexpected behavior when assigning to function #18877
Comments
This has nothing to do with the |
It seems you are right and that I was too quick to blame |
This is also not related to functions and is how scope supposed to work. julia> function f()
a
a = 1
end
f (generic function with 1 method)
julia> a = 2
2
julia> function g()
a
b = 1
end
g (generic function with 1 method)
julia> f()
ERROR: UndefVarError: a not defined
in f() at ./REPL[1]:2
julia> g()
1 |
And this is documented in http://julia.readthedocs.io/en/latest/manual/variables-and-scoping/#hard-local-scope under
PR to add more doc about this possibly to a more obvious place is welcome. |
Where else would this be documented? |
Thanks, once I realized that the assign statement was not involved it is quite understandable that this is the behavior. |
@mfalt Would you be willing to open a PR to document this? As someone who got caught by it you're a natural candidate to explain it well to others. If you'd like help/guidance for the process of doing a Julia doc PR we'd be happy to help you out. |
Record the 'scope' of the variable that was undefined (the Module, or a descriptive word such as :local or :static_parameter). Add that scope to the error message, and expand the hint suggestions added by the REPL to include more specific advice on common mistakes: - forgetting to set an initial value - forgetting to import a global - creating a local of the same name as a global - not matching a static parameter in a signature subtype Fixes #17062 (although more could probably be done to search for typos using REPL.string_distance and getting the method from stacktrace) Fixes #18877 Fixes #25263 Fixes #35126 Fixes #39280 Fixes #41728 Fixes #48731 Fixes #49917 Fixes #50369
Record the 'scope' of the variable that was undefined (the Module, or a descriptive word such as :local or :static_parameter). Add that scope to the error message, and expand the hint suggestions added by the REPL to include more specific advice on common mistakes: - forgetting to set an initial value - forgetting to import a global - creating a local of the same name as a global - not matching a static parameter in a signature subtype Fixes #17062 (although more could probably be done to search for typos using REPL.string_distance and getting the method from stacktrace) Fixes #18877 Fixes #25263 Fixes #35126 Fixes #39280 Fixes #41728 Fixes #48731 Fixes #49917 Fixes #50369
Record the 'scope' of the variable that was undefined (the Module, or a descriptive word such as :local or :static_parameter). Add that scope to the error message, and expand the hint suggestions added by the REPL to include more specific advice on common mistakes: - forgetting to set an initial value - forgetting to import a global - creating a local of the same name as a global - not matching a static parameter in a signature subtype Fixes #17062 (although more could probably be done to search for typos using REPL.string_distance and getting the method from stacktrace) Fixes #18877 Fixes #25263 Fixes #35126 Fixes #39280 Fixes #41728 Fixes #48731 Fixes #49917 Fixes #50369
I am not sure if this is a bug, but the following certainly creates some unexpected behavior (unexpected error message at least) and debugging headache where the assignment to length was supposed to be a test
length(b)==2
.which results in
especially when the assignment is not even reachable
Notice that the error occurs at the
length(a)
call, not in the assignment statements.Running Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
The text was updated successfully, but these errors were encountered: