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

Unexpected behavior when assigning to function #18877

Closed
mfalt opened this issue Oct 11, 2016 · 7 comments · Fixed by #51979
Closed

Unexpected behavior when assigning to function #18877

mfalt opened this issue Oct 11, 2016 · 7 comments · Fixed by #51979
Labels
docs This change adds or pertains to documentation Hacktoberfest Good for Hacktoberfest participants

Comments

@mfalt
Copy link
Contributor

mfalt commented Oct 11, 2016

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.

function f(a, b)
    N = length(a)
    length(b) = 2
    return N
end
f((1,2),(3,4))

which results in

ERROR: UndefVarError: length not defined
 in f(::Tuple{Int64,Int64}, ::Tuple{Int64,Int64}) at ./REPL[3]:2

especially when the assignment is not even reachable

function g(a, b)
    return length(a)
    length(b) = 2
end
g((1,2),(3,4))
ERROR: UndefVarError: length not defined
 in g(::Tuple{Int64,Int64}, ::Tuple{Int64,Int64}) at ./REPL[6]:2

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)

@yuyichao
Copy link
Contributor

This has nothing to do with the @assert. You introduced a local length function.

@mfalt
Copy link
Contributor Author

mfalt commented Oct 12, 2016

It seems you are right and that I was too quick to blame @assert, I updated my original post. It is however still unintuitive that a later command will affect a previous one.

@mfalt mfalt changed the title Unexpected behavior when assigning in assert Unexpected behavior when assigning to function Oct 12, 2016
@yuyichao
Copy link
Contributor

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

@yuyichao
Copy link
Contributor

And this is documented in http://julia.readthedocs.io/en/latest/manual/variables-and-scoping/#hard-local-scope under

An assignment introducing a variable used inside a function, type or macro definition need not come before its inner usage:

PR to add more doc about this possibly to a more obvious place is welcome.

@yuyichao yuyichao added the docs This change adds or pertains to documentation label Oct 12, 2016
@StefanKarpinski
Copy link
Sponsor Member

Where else would this be documented?

@mfalt
Copy link
Contributor Author

mfalt commented Oct 12, 2016

Thanks, once I realized that the assign statement was not involved it is quite understandable that this is the behavior.
It would be nice however to explicitly state somewhere in the docs that introducing a local variable turns all references to that variable to local references (even those occurring before the assignment) as shown in your example. I see no reason not to document this at the same place.

@kshyatt
Copy link
Contributor

kshyatt commented Oct 12, 2016

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

@fredrikekre fredrikekre added the Hacktoberfest Good for Hacktoberfest participants label Sep 28, 2017
vtjnash added a commit that referenced this issue Oct 31, 2023
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
vtjnash added a commit that referenced this issue Nov 8, 2023
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
vtjnash added a commit that referenced this issue Nov 8, 2023
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
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 Hacktoberfest Good for Hacktoberfest participants
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants