# Meta.parse + eval does not work as expected in the loop

**URL:** https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356
**Category:** New to Julia
**Created:** [November 11, 2021, 11:33pm UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356 "2021-11-11T23:33:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ryszard314159](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryszard314159/32/34402_2.png) [@ryszard314159](https://discourse.julialang.org/u/ryszard314159)
#### Post date: [November 11, 2021, 11:33pm UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/1 "2021-11-11T23:33:37Z")

</div>

I would expect the first for loop below to work, but apparently for some reason loop index (`n`) is not recognized in eval(xpr). I am guessing it has something to do with scoping etc.  
Is there a way to make it work “as expected” in this case i.e. to nudge `eval(Meta.parse())` to recognize loop index?

```julia
xpr = Meta.parse("3^n")
for n in 1:3 # ERROR: UndefVarError: n not defined
   println("n= ", n, ", xpr= ", xpr, ", eval(xpr)= ", eval(xpr))
end
for nn in 1:3 # this works as expected
   global n = nn
   println("n= ", n, ", 3^n= ", eval(Meta.parse("3^n")))
end

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [November 11, 2021, 11:51pm UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/2 "2021-11-11T23:51:41Z")

</div>

`eval` always always happens in the global scope, don’t do `eval` in loop

---

<div class="post-metadata">

### Author: ![ryszard314159](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryszard314159/32/34402_2.png) [@ryszard314159](https://discourse.julialang.org/u/ryszard314159)
#### Post date: [November 12, 2021, 1:12am UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/3 "2021-11-12T01:12:51Z")

</div>

I do not really understand potential ramifications of allowing `eval()` work in local scope as well, I guess, but it would be very handy. For now, with the silly `global n = nn` above I can do what I need to do.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 12, 2021, 1:16am UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/4 "2021-11-12T01:16:22Z")

</div>

`eval` working in the local scope is exactly why languages like python and R are impossible to optimize. In general, the fact that you are trying to do this implies you are doing something wrong.

---

<div class="post-metadata">

### Author: ![ryszard314159](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryszard314159/32/34402_2.png) [@ryszard314159](https://discourse.julialang.org/u/ryszard314159)
#### Post date: [November 12, 2021, 2:09am UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/5 "2021-11-12T02:09:27Z")

</div>

I would appreciate some suggestion what can I do to avoid using `eval()` in this case.  
Basically, I want the user of the function (invoked from the command line script) to be able to pass simple (but arbitrary) function (`ufun`) to evaluate some value dependent on the loop index. For example:

```julia
function foo(maxit, ufun)
  for n in 1:maxit
      v = ufun(n)
      [...]
  end
end

```

where `ufun(n)` can be e.g. `2^n`, `n^2`, `6*n`, etc.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 12, 2021, 2:16am UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/6 "2021-11-12T02:16:03Z")

</div>

parse the code, then construct a function at the global scope level, then in your code call that function.

```julia
expr = Meta.parse("x^2")
fdefexpr = :(x -> $(expr))
f = eval(fdefexpr)
f(2)

```

Be very careful about the fact that you are evaluating potentially arbitrary code provided by the user…

---

<div class="post-metadata">

### Author: ![ryszard314159](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryszard314159/32/34402_2.png) [@ryszard314159](https://discourse.julialang.org/u/ryszard314159)
#### Post date: [November 12, 2021, 1:22pm UTC](https://discourse.julialang.org/t/meta-parse-eval-does-not-work-as-expected-in-the-loop/71356/7 "2021-11-12T13:22:02Z")

</div>

Thanks a ton! It is very helpful. […]

> Be very careful about the fact that you are evaluating potentially arbitrary code provided by the user…

I am the only user so far… but definitely very good point!
