-
-
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
UndefVarError shadowing function #49917
Comments
Pinging @Ellipse0934 |
Seems to work as intended? |
The expectation is that shadowing works the same way it does in other languages, or that it provides an error message that helps you figure out what the problem is. |
It doesn't work the same. As the documentation says "The scope of a variable cannot be an arbitrary set of source lines; instead, it will always line up with one of these blocks.", which means its usable within its scope block before it is declared, but only after it has been given a value, so: # no s here
for i = 1:5
if i > 1
@show s
end
s = i
end is fine. Aside, I thought that this used to be clearer in the docs, but it seems to have been de-emphasised. |
function f(s, price)
println(f + 1)
f = s * price
end Produces the same error, for reasons which are obvious. (define (f s price)
(newline)
(display f)
(newline)
(define f (* s price))
(newline)
(display f)
(newline))
;; produces (in MIT/GNU Scheme)
1 ]=> (f 2 3)
#[compound-procedure 12 f]
6
;Unspecified return value But this makes sense given how bindings are resolved in Scheme -- function f(s, price)
@show f
s * price
end Has the same behavior. The semantics are little less obvious here since there is no I suppose one could wish for a more clever name-shadowing. Rust happily proceeds with the example. Python returns what is arguably a more informative error message. def f(s, price):
print(f)
f = s * price
print(f)
# and we get
>>> f(2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
UnboundLocalError: cannot access local variable 'f' where it is not associated with a value @elextr Given that Julia's let u
# u exists in environment, but is unassigned
let v
# v exists in environment, but is unassigned
let a = 2
let b = 3
# now we assign values to u and v. If this were Scheme, we'd use `set!`
u = a + b
v = a * b
end
end
println(u + v) # prints 11
end
# println(v) # this will error as v is now out of scope
println(u) # prints 5
end |
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
The bug:
User's
versioninfo()
:julia> versioninfo() Julia Version 1.9.0 Commit 8e630552924 (2023-05-07 11:25 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) CPU: 24 × AMD Ryzen 9 7900X 12-Core Processor WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-14.0.6 (ORCJIT, znver3) Threads: 69 on 24 virtual cores
I also replicated this on my own machine on master.
The text was updated successfully, but these errors were encountered: