eval, by design, doesn't work that way. there are just too many better alternatives. typically, an anonymous function / lambda is the best and most direct replacement:
let x=2 println(x) # Line 1 exp = () -> x+1 println(exp()) # Line 2 end On Wed Dec 10 2014 at 10:43:00 PM Michael Mayo <mm...@waikato.ac.nz> wrote: > Hi folks, > > I have the following code fragment: > > x=1 > let x=2 > println(x) # Line 1 > exp=:(x+1) > println(eval(exp)) # Line 2 > end > > It contains two variables both named x, one inside the scope defined by > let, and one at global scope. > > If I run this code the output is: > 2 > 2 > > This indicates that (i) that line 1 is using the local version of x, and > (ii) that line 2 is using the global version of x. > > If I remove this global x I now get an error because eval() is looking for > the global x which no longer exists: > > let x=2 > println(x) # Line 1 > exp=:(x+1) > println(eval(exp)) # Line 2 > end > > 2 > > ERROR: x not defined > > > My question: when evaluating an expression using eval() such as line 2, > how can I force Julia to use the local (not global) version of x and thus > avoid this error? > > > Thanks > > Mike >