# Passing a string into function as a expression

**URL:** <https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 28, 2019, 2:52pm UTC](https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625 "2019-11-28T14:52:54Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 28, 2019, 10:33pm UTC](https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625/14 "2019-11-28T22:33:50Z")

</div>

First, those who say that you should avoid evaluating expressions at runtime are absolutely correct. Don’t do it if there’s any other solution.

If you do need to do it, and there are situations where it is valid, you have two sane options to make it semantically local, even though the `eval` takes place in global scope.

1. Eval an anonymous function and pass your arguments to it.

```julia
julia> function Compare(a, b, c::String)
           f = eval(Meta.parse("(a, b) -> $c"))
           return Base.invokelatest(f, a, b)
       end
Compare (generic function with 1 method)

julia> Compare(1, 1, "a>b")
false

```

The `invokelatest` call does the same thing as `f(a, b)` but is necessary for technical reasons, basically because it calls a function that didn’t exist when `Compare` itself was called.

1. Inject your arguments into the expression before evaluating it. This has been demonstrated in earlier replies but let’s do it in a more general way, reusing a solution from [Evaluate expression with variables from a dictionary - #2 by GunnarFarneback](https://discourse.julialang.org/t/evaluate-expression-with-variables-from-a-dictionary/30047/2) :

```julia
interpolate_from_dict(ex::Expr, dict) = Expr(ex.head, interpolate_from_dict.(ex.args, Ref(dict))...)
interpolate_from_dict(ex::Symbol, dict) = get(dict, ex, ex)
interpolate_from_dict(ex::Any, dict) = ex
function Compare(a, b, c::String)
    expr = Meta.parse(c)
    return eval(interpolate_from_dict(expr, Dict(:a => a, :b => b)))
end

```

---

_[View the full topic](https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625)._
