# Macro: extract the method name, arguments and the body

**URL:** <https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343>\
**Category:** General Usage\
**Created:** [February 17, 2024, 1:57pm UTC](https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343 "2024-02-17T13:57:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 17, 2024, 1:57pm UTC](https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343/1 "2024-02-17T13:57:02Z")

</div>

There are two syntaxes that I know that defines a function:

```julia
julia> Meta.@dump function f(x)
       x
       end
Expr
  head: Symbol function
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol f
        2: Symbol x
    2: Expr
      head: Symbol block
      args: Array{Any}((3,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[42]
        2: LineNumberNode
          line: Int64 2
          file: Symbol REPL[42]
        3: Symbol x

julia> Meta.@dump f(x)=x
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol f
        2: Symbol x
    2: Expr
      head: Symbol block
      args: Array{Any}((2,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[43]
        2: Symbol x

```

when there is a `where` statement things just get more complicated. It’s common for a macro to take a function and manipulate its name, arguments and body. Is there a built-in method that extracts those information?

Further, when I use a macro before a function definition, the macro gets a collection of expressions. How do I merge them like how `Meta.@dump` does?

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 17, 2024, 2:12pm UTC](https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343/2 "2024-02-17T14:12:04Z")

</div>

My solution for the second problem is

```julia
macro mymacro(expr...)
    mymacro(Meta.@dump(expr...))
end

```

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [February 17, 2024, 6:47pm UTC](https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343/3 "2024-02-17T18:47:09Z")

</div>

Probably not exactly the answer to your questions, but MacroTools.jl provide some useful, well, macro tools. You can convert (normalize) function definitions to short or long form, among other things.

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 21, 2024, 10:00pm UTC](https://discourse.julialang.org/t/macro-extract-the-method-name-arguments-and-the-body/110343/4 "2024-02-21T22:00:44Z")

</div>

Just found ExprTools.jl that does the job.
