# Passing expression stored in variable into a macro

**URL:** <https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [November 8, 2018, 3:55pm UTC](https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311 "2018-11-08T15:55:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [November 8, 2018, 3:55pm UTC](https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311/1 "2018-11-08T15:55:02Z")

</div>

At some point in my program I create expressions (and store them inside variable (which is inside a struct)). At some later point I wish to do some light manipulations of these expressions and execute the resulting code. How do I do this?

Sample Code:

```julia
ex = :(F(x)=2x+1)
macro inc1(ex_func)
    exx_func = deepcopy(ex_func)
    exx_func.args[2].args[2].args[3] += 1
    return exx_func
end
...
@inc1 ex

```

Here `inc1` only sees the symbol `:ex`, likvise if I try `$ex` it only sees that, but not the content of the expression `ex`.

Is this even possible/recommended with macros? I can achieve the same thing with a normal function and then an `eval` at the end. However, when I first started with metaprogramming I learnt something about eval’s being dangerous and to avoid them.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 8, 2018, 5:45pm UTC](https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311/2 "2018-11-08T17:45:14Z")

</div>

No, what you’re trying to do is not possible with a macro. Macros _only_ operate on the _literal syntax_ they receive, so the symbol `:ex` is all the macro has access to. On the other hand, it’s perfectly fine to write a function that operates on expressions, and there’s nothing inherently wrong with using `eval()`. If you can provide more information about what you’re trying to do, we may be able to provide more specific advice about appropriate usage of `eval()`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 8, 2018, 5:50pm UTC](https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311/3 "2018-11-08T17:50:35Z")

</div>

Wrote this but apparently didn’t subbmit it, rdeits wrote pretty much the same thing. Anyway:

Macros are syntactic transformations which doesn’t seem like what you want here.

> [@Torkel](#):
>
> However, when I first started with metaprogramming I learnt something about eval’s being dangerous and to avoid them.

eval can be dangerous but that is precisely because it allows you to dynamically execute arbitrary code (which it seems is what you need here).

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [November 8, 2018, 7:44pm UTC](https://discourse.julialang.org/t/passing-expression-stored-in-variable-into-a-macro/17311/4 "2018-11-08T19:44:30Z")

</div>

Thank you.

`DifferentialEquations.jl` have several modelling tools which allows you to create models which are stored in structs and which can be used as input to simulations problems. One of these, `DiffEqBiological`, generates a couple of expressions on use and stores them in the struct. I want to make a tool which modifies these expressions (which essentially are functions) and then evaluates them.
