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
eval doesn't have access to variables in function scope #2386
Comments
See also #1651. |
A possible workaround is to use a macro and |
Eval always evaluates in global scope, since it's essentially impossible to JIT compile eval. What is your usecase?
|
I don't care if it is JIT compiled. I just need it to be evaluated in the correct scope. I can't use a macro because macros evaluate at parse-time rather than run-time, and I need the latter here. I can use global variables to work around this, but that is a total hack. This is a bug. I'm using it for my PyCall module, in order to have a function that generates an anonymous composite type to wrap around around a Python object: using introspection, it determines the object's methods, and generates the corresponding structure fields. This way I can do e.g.
|
It's a bit hard to quickly dig into the PyCall code to determine if this is reasonable, but you can embed the values of the variables you need in the expression, e.g.: julia> bar(y) = @eval $y
# method added to generic function bar
julia> bar(7)
7 |
This is not a bug, because it was a deliberate choice to make it possible to analyze local variables. I think there are at least two good ways to deal with this use case: insert the values of |
Ah, I didn't realize that one could insert non- Okay, I see why it might be impractical to lookup local symbols, and why it is not needed. |
@mlubin: Though you should write that as
otherwise it will not work as expected for e.g. symbols, and might fail for other types of |
I guess that would be
or does |
I get the impression that not having to wrap an Array around |
Perhaps. I need to be sure that it constructs a single argument expr |
Sorry about the sudden change, but I think it will be better to remove |
Thanks for that clarification! |
Ok, seems like a good change. So |
Then I agree with you, @pao. Feel free to do it if you like. |
Correct, it will always be a single-arg expression. |
I have an expression that I want to differentiate using the Calculus package, and then create a Julia function that evaluate the expression of the derivative at a given value of x, like in the example: using Calculus The last function definition is broken by this issue: julia> f(8) How am I supposed to do this in Julia? |
Please ask questions on https://discourse.julialang.org |
The following works:
But this gives an error:
It makes no sense to me that
eval
does not evaluate its expressions in the scope where it is called (and the manual seems to indicate this as well).The text was updated successfully, but these errors were encountered: