Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia local variable not defined in expression eval

Given the following function,

function floop(exp)
    a = 5
    b = 10
    ex = Expr(:call, :+, 1, exp);
    return eval(ex);
end

if I then run

floop(Symbol("b"))

I get an error saying that b is not defined. Why does this not work? How can I make this work?

like image 308
Grayscale Avatar asked Feb 07 '20 01:02

Grayscale


Video Answer


1 Answers

One of the key things that lets Julia be fast is that eval always runs in the global scope. This means it can never refer to local variables.

like image 184
Oscar Smith Avatar answered Sep 24 '22 08:09

Oscar Smith