# Metaprogramming using Meta.parse + eval

**URL:** <https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014>\
**Category:** New to Julia\
**Tags:** metaprogramming, turing\
**Created:** [February 22, 2020, 12:05pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014 "2020-02-22T12:05:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![terdmann](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@terdmann](https://discourse.julialang.org/u/terdmann)\
**Post date:** [February 22, 2020, 12:05pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/1 "2020-02-22T12:05:27Z")

</div>

Here’s an example of what I am trying to do:

```julia
src = ["function f(x) x^2 end",
         "function f(x) x^3 end",
         "function f(x) x^4 end"]
for s in src
    eval(Meta.parse(s))
    @show f(2)
end

```

This does not work.

The context is this: I want to automatically define multiple models that share a lot of code, so I am constructing their source as a string.  
Now I want to loop over the functions (or their definitions) and fit them to data.

How can I actually do this? Or should I use a different approach?

---

<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:** [February 22, 2020, 12:58pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/2 "2020-02-22T12:58:09Z")

</div>

> [@terdmann](#):
>
> This does not work.

What do you mean it doesn’t work?

You are not creating multiple functions but that code should “work” as you are overriding the methods.

> [@terdmann](#):
>
> The context is this: I want to automatically define multiple models that share a lot of code, so I am constructing their source as a string.

No matter what you need, you never need to construct their code as string. For sharing code, use a common function that all of them calls. For shared template, which your string approach couldn’t deal with anyway, splice your variable part into the code directly. In all cases, you should do metaprogramming on code not strings.

> [@terdmann](#):
>
> Now I want to loop over the functions (or their definitions) and fit them to data.

And if you meant after you did this loop you only have one `f`. Well, you need to create multiple functions instead of one. Either give them different names or use anonymous functions.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [February 22, 2020, 12:59pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/3 "2020-02-22T12:59:41Z")

</div>

Welcome!  
You can use @eval to define programmatically functions without having to use strings, here is a code snippet:

```julia
import Base: +, -, *, /, ^
operations = (:+, :-, :*, :/, :^)
for op in operations
    @eval $op(c1:: MyType, a:: Number) = MyType(c1.x, $op.(c1.y, a))
end

```

Edit: added a reason for my post in the first line.

---

<div class="post-metadata">

**Author:** ![terdmann](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@terdmann](https://discourse.julialang.org/u/terdmann)\
**Post date:** [February 22, 2020, 1:10pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/4 "2020-02-22T13:10:37Z")

</div>

Sorry for being unclear: what I mean is that the output of

```julia
src = ["function f(x) x^2 end",
       "function f(x) x^3 end",
       "function f(x) x^4 end"]
for s in src
    eval(Meta.parse(s))
    @show f(2)
end

```

is not 4, 8, 16.

You are right though and I should find a way to define the models differently.

---

<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:** [February 22, 2020, 1:12pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/5 "2020-02-22T13:12:51Z")

</div>

```julia
julia> src = ["function f(x) x^2 end",
                "function f(x) x^3 end",
                "function f(x) x^4 end"]
3-element Array{String,1}:
 "function f(x) x^2 end"
 "function f(x) x^3 end"
 "function f(x) x^4 end"

julia> for s in src
           eval(Meta.parse(s))
           @show f(2)
       end
f(2) = 4
f(2) = 8
f(2) = 16

```

It probably still won’t do what you want in other cases but I don’t see why this case won’t. More like you get a `f` somewhere / you didn’t paste the full code.

---

<div class="post-metadata">

**Author:** ![terdmann](https://avatars.discourse-cdn.com/v4/letter/t/85e7bf/32.png) [@terdmann](https://discourse.julialang.org/u/terdmann)\
**Post date:** [February 22, 2020, 1:24pm UTC](https://discourse.julialang.org/t/metaprogramming-using-meta-parse-eval/35014/6 "2020-02-22T13:24:22Z")

</div>

Oh dear… yes in a fresh session it works… sorry for the confusion on my part.
