-
-
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
type inference does not work with closures #9248
Comments
See #1864 |
In this case, julia> T = Float64
Float64
julia> g1(x) = convert(T, x)
g1 (generic function with 1 method)
julia> g1(1)
1.0
julia> T = Float16
Float16
julia> g1(1)
float16(1.0) |
See also #4428 |
Thanks for looking into this. One observations though following @mbauman comment. I thought that if I wrap the functions inside a function it should have the same effect as declaring function uu(T)
g1(x) = convert(T, x)
g11() = x
g11(x) = convert(T, x)
g2(x,T) = convert(T, x)
return g1, g11, g2
end
T = Float64
g1, g11, g2 = uu(T)
@code_typed g1(5) # inferred return type Any
@code_typed g11(5) # inferred return type Any
@code_typed g2(5,T) # inferred return type Float64 But Julia still can't infer the return types, shouldn't that work? |
I think that's due to a specialization heuristic. If you use |
Yes that works. Before posting above I tried with |
In below example the return type is only inferred when
T
is passed into the function but not when it's passed in through a closure (both for 0.3.2 and 0.4):Now that all objects can be callable it could be pretty natural to use these "extra functions" like normal function, i.e. as closures. A contrived example:
Issue #175 is the only one I found which references type inference and closures.
The text was updated successfully, but these errors were encountered: