# Converts the given string into code with metaprogramming

**URL:** <https://discourse.julialang.org/t/converts-the-given-string-into-code-with-metaprogramming/78819>\
**Category:** General Usage\
**Tags:** metaprogramming, strings\
**Created:** [March 31, 2022, 3:41pm UTC](https://discourse.julialang.org/t/converts-the-given-string-into-code-with-metaprogramming/78819 "2022-03-31T15:41:03Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [April 1, 2022, 9:33am UTC](https://discourse.julialang.org/t/converts-the-given-string-into-code-with-metaprogramming/78819/6 "2022-04-01T09:33:35Z")

</div>

Apart from “please try to avoid doing this in the first place” this is hard to answer properly without knowing how it’s going to be used and how the example is supposed to generalize.

Here is another approach that may or may not be useful:

```julia
julia> function foo(s, x)
           f = eval(Meta.parse("x -> $s"))
           return Base.invokelatest(f, x)
       end
foo (generic function with 1 method)

julia> foo("2x+1", 3)
7

```

Cf. [Passing a string into function as a expression - #14 by GunnarFarneback](https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625/14)

---

_[View the full topic](https://discourse.julialang.org/t/converts-the-given-string-into-code-with-metaprogramming/78819)._
