-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 with closures defined as named function
s
#39484
Comments
In the second example I note it has created entirely seperate closures, too. At top-level I can do things like f(x::Missing) = missing
if y
f(x::Number) = x + 1
else
f(x::Number) = x
end and this would end up with one function Inside a function the equivalent would be: function g(y::Bool)
f(x::Missing) = missing
if y
f(x::Number) = x + 1
else
f(x::Number) = x
end
return f
end I'm guessing it's not trivial to ask lowering to do what top-level did (basically, this might be to create two seperate closures with two methods each, and return one or the other depending on the branch). |
So here's an example of the above. julia> function incrementer(z::Number)
g(x::Missing) = missing
if z > 0
g(x::Number) = x + z
else
g(x::Number) = x - z
end
return g
end
incrementer (generic function with 1 method)
julia> incrementer(1)
(::var"#g#14"{Int64}) (generic function with 2 methods)
julia> incrementer(-1)
(::var"#g#14"{Int64}) (generic function with 2 methods)
julia> incrementer(-1)(0)
1
julia> incrementer(1)(0)
-1 Again I have no idea if this is even possible to "fix". Perhaps the fix to this issue is to have some kind of lowering error in these cases? Or at least a method replacement warning? |
As a closure, it is equivalent to:
|
Yes, that basically makes sense. I assume lowering basically strips out all the methods it finds and attaches it to the closure. Two questions, though:
|
The Other than that, I agree it would be nice to have an error when closure methods are repeated, I can't think of a reason that would be a good idea. Unfortunately that can't always be done in lowering — duplicate methods could occur if the user wrote different names for the same types — so if we do it as part of lowering we will miss some cases. |
Sure. In @vtjnash's pseudo-lowering I'd expect any duplicate method warning would happen when those methods are processed for |
Dup of #15602? |
Yes, the OP is precisely #15602 (comment) and the rest of the discussion here mirrors there. Thanks @KristofferC for pointing me in the right direction. |
I have run into an unexpected problem with (a more complicated version of) the following code:
I had assumed that closure notation
function f(x) ... end
was intended to be equivalent tof = (x) -> ...
(well, the former may allow you to attach an addition method to the function or closure, also). Lowering has generated the following code:It seems to have removed the second closure assignment entirely. I expected it to behave like the following assignment form (even with "long" function notation):
The text was updated successfully, but these errors were encountered: