# Expression Updating

**URL:** https://discourse.julialang.org/t/expression-updating/123135
**Category:** General Usage
**Created:** [November 27, 2024, 12:33am UTC](https://discourse.julialang.org/t/expression-updating/123135 "2024-11-27T00:33:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)
#### Post date: [November 27, 2024, 12:33am UTC](https://discourse.julialang.org/t/expression-updating/123135/1 "2024-11-27T00:33:06Z")

</div>

Say I have an expression block like the following:

```julia
quote
    x = foo(args...)
    y = otherpackage.bar(args...)
end

```

I would like to turn that into the following:

```julia
quote
    x = Module.foo(args...)
    y = Module.otherpackage.bar(args...)
end

```

This is a pretty simple example, but I want it to be robust and be able to parse all function calls and stick the `Module` in front of them.

For reference, I am trying to fix this issue in [Handcalcs Function Limitations](https://co1emi11er2.github.io/Handcalcs.jl/stable/tutorial/#An-example-for-rendering-expressions-within-a-function:). You will have to scroll down to the bottom of the section linked. The linked issue I am referring to:

- _If the function has other function calls within it’s body that are not available in Main, then the macro will error._

---

<div class="post-metadata">

### Author: ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)
#### Post date: [November 27, 2024, 6:57am UTC](https://discourse.julialang.org/t/expression-updating/123135/3 "2024-11-27T06:57:09Z")

</div>

I would suggest to use MacroTools.jl for this task (see the [docs](http://fluxml.ai/MacroTools.jl/stable/pattern-matching/)). So your example might look like this:

```julia
using MacroTools: @capture, prewalk

ex = quote
    x = foo(args...)
    y = otherpackage.bar(args...)
end

prewalk(ex) do x
    if @capture(x, f_(args__))
        :(Main.($f)($(args...)))
    else
        x
    end
end

```

which returns:

```julia
quote
    x = Main.(foo)(args...)
    y = Main.(otherpackage.bar)(args...)
end

```

EDIT: it is probably better to use `prewalk` here to be able to replace nested function calls as well.

---

<div class="post-metadata">

### Author: ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)
#### Post date: [November 27, 2024, 3:35pm UTC](https://discourse.julialang.org/t/expression-updating/123135/4 "2024-11-27T15:35:39Z")

</div>

This is awesome! Thanks!
