# Evaluate expression in function

**URL:** <https://discourse.julialang.org/t/evaluate-expression-in-function/24402>\
**Category:** New to Julia\
**Created:** [May 20, 2019, 1:49pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402 "2019-05-20T13:49:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [May 20, 2019, 1:49pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/1 "2019-05-20T13:49:55Z")

</div>

I have constructed an function like follows:

```julia
function f(a::Vector{Float64})
   for i = 1:length(a)
      b = eval(Meta.parse(string("a[",i,"]")))
      println(b)
   end
end

```

Getting this error when I run test

```julia
c = rand(5);

f(c);

UndefVarError: a not defined

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 20, 2019, 1:56pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/2 "2019-05-20T13:56:47Z")

</div>

`eval` evaluates in global scope (where you don’t have `a`):

```julia-repl
help?> eval

  eval(expr)

  Evaluate an expression in the global scope of the containing module.
  Every Module (except those defined with baremodule) has its own
  1-argument definition of eval, which evaluates expressions in that
  module.

```

What are you trying to do? Typically `eval(parse(...))` should be avoided, there are probably better ways to do what you want.

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [May 20, 2019, 1:59pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/3 "2019-05-20T13:59:01Z")

</div>

I don’t know what you want to do but something like this

`b = eval(Meta.parse(string("$(a[i])")))`

but for what do you need eval there ?

---

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [May 20, 2019, 3:03pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/4 "2019-05-20T15:03:52Z")

</div>

@mwolff, @fredrikekre Thank you so much for your help.

I am creating a data structure that contains a set of fields

```julia
struct point
   x :: Float64
   y :: Float64
   z :: Integer
end

```

The fields of the structure are used to create expressions.

e.g.

```julia
p = point(1.5,2.5,6)

expr = String[];
push!(expr, "p.x + p.y*5");
push!(expr, "p.x*p.z + p.y*5");
push!(expr, "sin(p.x) + p.y*5");
...

```

I need to evaluate expressions within a function

`values = f(p,expr)`

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [May 20, 2019, 3:20pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/5 "2019-05-20T15:20:09Z")

</div>

ok this could help a bit

```julia
value=Meta.parse("$(sin(p.x) + p.y * 5)")
println(value)

```

best regards

Michael

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 20, 2019, 3:27pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/6 "2019-05-20T15:27:20Z")

</div>

> [@Elmer\_Cusipuma](#):
>
> struct point x :: Float64 y :: Float64 z :: Integer end

Best not to use `eval` when there’s a much easier way to get what you want:

```julia
exprs = Function[]
append!(exprs, [p -> p.x + 5p.y, 
              p -> p.x*p.z + 5p.y,
              p -> sin(p.x) + 5p.y])

p = point(1.5, 2.5, 6)
f(p, exprs) = [expr(p) for expr in exprs]

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 20, 2019, 3:29pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/7 "2019-05-20T15:29:11Z")

</div>

The main question is why do you need to do that, why do you have to use strings, and why do you need to do that in a function, also what syntax do you want to support. As usual for these kind of question, the solution greately depend on what you actually need.

2 more minor comments,

1. Don’t use strings, at least use expressions, unless it’s interactive user input (i.e. REPL)
2. 

> [@mwolff](#):
>
> value=Meta.parse(“$(sin(p.x) + p.y \* 5)”)

Is not doing any metaprogramming.

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [May 20, 2019, 3:58pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/8 "2019-05-20T15:58:54Z")

</div>

You are right, for fixed formulas, of course, this is the better way. But if someone sends me a formula over the network as a string and I want a result right away, that’s a possibility. Many roads lead to Rome…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 20, 2019, 4:17pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/9 "2019-05-20T16:17:54Z")

</div>

No, for fixed expressions you should have just write that expressions. Turning the result to a string and back doesn’t buy you anything…

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 20, 2019, 4:24pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/10 "2019-05-20T16:24:21Z")

</div>

> [@mwolff](#):
>
> But if someone sends me a formula over the network as a string […]

then someone will send you a string that evals to a shell on your box.

edit: What you want is a small DSL with robust parser. I don’t know of any good packages that help you with this; but julia macros or eval are not gonna help you here. You should especially prevent contact between the julia parser / deserialization (including jld2) and attacker-supplied data. Even better, ask around whether there are already well-established DSLs supporting the set of features you need, and then write (or import or wrap) a parser for it. After running things through your own parser, you can generate and compile an expression tree if necessary.

---

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [May 20, 2019, 5:17pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/11 "2019-05-20T17:17:57Z")

</div>

@stillyslalom Thank you so much for your help.

The inputs are of type string.  
How can I convert string expressions and save them in a function array?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 20, 2019, 5:58pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/12 "2019-05-20T17:58:16Z")

</div>

```julia
expr_strings = "[p -> p.x + 5p.y, 
                 p -> p.x*p.z + 5p.y,
                 p -> sin(p.x) + 5p.y]"

exprs = include_string(Base, expr_strings)

```

\* edit: it’s better hygiene to work in your own module and reduce risk of a user inadvertently overwriting `Base` methods, in which case you’d use `include_string(MyModule, expr_strings)`.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [May 20, 2019, 11:49pm UTC](https://discourse.julialang.org/t/evaluate-expression-in-function/24402/13 "2019-05-20T23:49:11Z")

</div>

Maybe [https://github.com/JuliaDiffEq/DiffEqOnlineServer](https://github.com/JuliaDiffEq/DiffEqOnlineServer) could be a useful starting point (particularly `sanitation.jl`)
