-
-
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
function declarations in local scope have inconsistent behaviour #34910
Comments
try
blocks have inconsistent behaviour
Not sure what the best label for this is, but does still reproduce in 1.8.3 and 1.9.0-alpha1 |
Yes, still the same behaviour on 1.8. It's very much a corner case. I don't know what the outcome should be TBH... |
This is even worse with conditional function declarations julia> (let
if true
f() = 1
else
f() = 2
end
end)()
WARNING: Method definition f() in module Main at REPL[66]:3 overwritten at REPL[66]:5.
2
julia> (let
if false
f() = 1
else
f() = 2
end
end)()
WARNING: Method definition f() in module Main at REPL[67]:3 overwritten at REPL[67]:5.
ERROR: UndefVarError: `f` not defined
Stacktrace:
[1] top-level scope
@ REPL[67]:5 from #5148 (comment) |
For the first conditional case, IIUC |
They are in the same scope, but code in the else branch of an julia> if true
f() = 1
else
f() = 2
end
f (generic function with 1 method)
julia> f()
1 |
On thinking about it some more, and based on my understanding as expressed on #47733 declarations happen irrespective of execution, a declaration after a return still exists throughout the scope even though its never executed. So that applies to function declarations/method definitions as well. So the But what the |
Our posts overlapped, but I understand that the rules of REPL globals are different because its not really a "scope". |
Also from #5148 (comment): julia> function fn_generator3()
if rand(Bool)
f() = 0
else
f(x, y) = x+y
end
end
fn_generator3 (generic function with 1 method)
julia> fn_generator3()
(::var"#f#4") (generic function with 2 methods)
julia> fn_generator3()
ERROR: UndefVarError: f not defined
julia> fn_generator3()
(::var"#f#4") (generic function with 2 methods) and julia> i=3
3
julia> function f1()
i=5; g() = 1
for i=1:3
g() = @show(i); g() # ok so we've defined `i` at local scope, parent scope, and global
end
g
end
WARNING: Method definition g() in module Main at REPL[126]:2 overwritten at REPL[126]:4.
f1 (generic function with 1 method)
julia> f1()() # where is `i` *not* defined!?
ERROR: UndefVarError: i not defined As suggested in the #5148 comment, I think the solution is to throw errors on |
I just realized that The OP case seems to do with declaring local functions that type-specialize on locally-defined types. For example: julia> b=try
bar = "Hello, world!"
h = (::typeof(bar)) -> 934
finally end
ERROR: UndefVarError: `bar` not defined |
Meanwhile: julia> function h end
h (generic function with 0 methods)
julia> b=try
bar = "hello, world!"
h(::typeof(bar)) = 934
finally end
h (generic function with 1 method)
julia> h("hi")
934 |
This code fails on Julia 1.3:
However, commenting either the second or third line of the try block makes it succeed. It seems to me that either
function bar end
should be an error outside of the top-level, or the above code should work... FWIW it came about because I have a macro that works at the top-level, but fails inside a@testset
. I can pull it out, but it made sense to have it inside.The text was updated successfully, but these errors were encountered: