i = 50
function test()
i = 10
eval(:i)
end
test() # => 50
Why does this evaluate to the global i
instead of the local one? Is there a way to make it evaluate to the local?
A return type can be specified in the function declaration using the :: operator. This converts the return value to the specified type. This function will always return an Int8 regardless of the types of x and y .
In strict mode, declaring a variable named eval or re-assigning eval is a SyntaxError . If the argument of eval() is not a string, eval() returns the argument unchanged. In the following example, the String constructor is specified and eval() returns a String object rather than evaluating the string.
For any assignment to a global, Julia will first try to convert it to the appropriate type using convert : julia> global y::Int julia> y = 1.0 1.0 julia> y 1 julia> y = 3.14 ERROR: InexactError: Int64(3.14) Stacktrace: [...]
= is the assignment operator, and val and _val are variables.
You can't. Julia's eval
always evaluates code the current module's scope, not your local scope. Calling eval
in local scope is an anti-pattern and a performance killer. You can, however, construct a new function which includes a bit of user code that refers to local scope and then call that function. For example:
# user-supplied expression
expr = :(i^2 + 1)
# splice into function body
test = @eval function ()
i = 10
$expr
end
Now you can call test
:
julia> test()
101
Why is this better than calling eval
inside of test
as in the question? Because in the original, eval
needs to be called every time you call test
whereas in this usage, eval
is only called once when defining test
; when test
is called no eval
is done. So while you may call eval
"at run time" in the sense of "while your application is running, getting user input" it is not called "at run time" in the sense of when the function constructed with that user input is called. The former is fine, whereas the latter as distinctly an anti-pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With