# How do Julia convert a Expr to another Expr?

**URL:** <https://discourse.julialang.org/t/how-do-julia-convert-a-expr-to-another-expr/79208>\
**Category:** New to Julia\
**Tags:** metaprogramming\
**Created:** [April 8, 2022, 6:17am UTC](https://discourse.julialang.org/t/how-do-julia-convert-a-expr-to-another-expr/79208 "2022-04-08T06:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 8, 2022, 6:17am UTC](https://discourse.julialang.org/t/how-do-julia-convert-a-expr-to-another-expr/79208/1 "2022-04-08T06:17:00Z")

</div>

How do Julia convert a Expr to another Expr?

```julia
g1=:(_+2)

g2=:(x->x+2)

```

How can I convert g1 to g2?

I know I can do it in this way:

```julia

"x->"*replace(string(g1),"_"=>"x") |> Meta.parse

```

But this way is tedious, I would thought there is a more elegant way?

By the way, my target is to define a maro, which permit pip function with underscore replacing.  
More infomation see there:  
[https://github.com/JuliaLang/julia/pull/24990](https://github.com/JuliaLang/julia/pull/24990)

Is it possible to setdown for this issue in near future?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 8, 2022, 6:32am UTC](https://discourse.julialang.org/t/how-do-julia-convert-a-expr-to-another-expr/79208/2 "2022-04-08T06:32:28Z")

</div>

I find the best way to do `Expr` manipulation to investigate the expression you have, and the one you want, with [`dump`](https://docs.julialang.org/en/v1/base/io-network/#Base.dump) and then work from there:

```julia
julia> dump(:(_ + 2))
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol +
    2: Symbol _
    3: Int64 2

julia> dump(:(x -> x + 2))
Expr
  head: Symbol ->
  args: Array{Any}((2,))
    1: Symbol x
    2: Expr
      head: Symbol block
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[35]
        2: Expr
          head: Symbol call
          args: Array{Any}((3,))
            1: Symbol +
            2: Symbol x
            3: Int64 2

```

Here is a very simple function to do this transformation. It doesn’t handle complicated inputs (e.g. doesn’t recurse into nested `Expr` etc) but hopefully it should get you started:

```julia
julia> function to_func(ex)
           # Get a unique symbol for the argument
           s = gensym()
           # Replace _ with s
           # TODO: This doesn't handle e.g. nexted Expr etc and needs refinement
           for i in 1:length(ex.args)
               if ex.args[i] === :_
                   ex.args[i] = s
               end
           end
           # Construct return expression
           rex = Expr(:->, s, Expr(:block, ex))
           return rex
       end
to_func (generic function with 1 method)

julia> dump(to_func(:(_ + 2)))
Expr
  head: Symbol ->
  args: Array{Any}((2,))
    1: Symbol ##300
    2: Expr
      head: Symbol block
      args: Array{Any}((1,))
        1: Expr
          head: Symbol call
          args: Array{Any}((3,))
            1: Symbol +
            2: Symbol ##300
            3: Int64 2

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [April 8, 2022, 7:19am UTC](https://discourse.julialang.org/t/how-do-julia-convert-a-expr-to-another-expr/79208/3 "2022-04-08T07:19:32Z")

</div>

That is a more readable function solution, as I can imagine, it is easy to extend for nested Expr.  
Thank you so much.
