-
-
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
Generated functions as closures #18747
Comments
This issue seems to apply to inner generated functions generally: julia> function f(x)
@generated g() = x
g
end;
julia> f(1)()
ERROR: type DataType has no field x
in g(...) at ./REPL[1]:2 |
Oh, that's nice to know. I just tried something like this and it worked: function f(x)
@generated function()
x
end
end julia> f(1)()
1 So I assumed it was a macro thing. I will update the issue, then. |
I'm not sure how easily this can be fixed, but the correct answer should have been |
@vtjnash yeah, I later realized that the above gave a f = @generated function() end
g = @generated function h() end julia> methods(f).ms[1].isstaged
false
julia> methods(g).ms[1].isstaged
true |
@vtjnash hey wait, you sure? The " x = 1
@generated f() = x julia> f()
1
julia> methods(f).ms[1].isstaged
true |
Yes, I think it would be reasonable for generated functions to still capture variables by value. Arguably To give a reason, functions are not specialized on the types of captured variables, unless those are reflected in the type of the function itself, which is already an argument. So generated functions do not need their captured variables to become types. |
True, this is perhaps one way we could allow inner generated functions to capture variables, by requiring that the generator also reference that variable? The implementation of such sounds quite annoying. However, the following would need to hold in order for the cached code to have only depended on the type function g(x)
@generated f()
return :(x :: $x)
end
return f
end |
In your code example, x = 1
@generated f() = x you are missing a |
As performed in
0.5.0
and0.6.0-dev.826
.:?
The text was updated successfully, but these errors were encountered: