# Trying to write a macro for simple debugging

**URL:** <https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943>\
**Category:** General Usage\
**Tags:** macros, debugging\
**Created:** [February 7, 2022, 10:42am UTC](https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943 "2022-02-07T10:42:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcsj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcsj/32/18526_2.png) [@bcsj](https://discourse.julialang.org/u/bcsj)\
**Post date:** [February 7, 2022, 10:42am UTC](https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943/1 "2022-02-07T10:42:20Z")

</div>

I’m trying to wrap my head around macros and wanted to make (what I assumed is) a simple macro which you prefix to an expression in order to get the expression printed with its evaluated value.

**Example** Let us call the macro `@db`. What I imagine is the following:

```julia
Case 1
--------------
@db 2 + 2
2 + 2 = 4

Case 2
--------------
f(a,b) = begin
  @db a + b
end
f(2, 2)
a + b = 4

```

It seems to me that I need something along the following lines. But this version doesn’t work!

```julia
macro db(ex::Expr)
  :(print($(string(ex)) * " = " * string(eval($ex))))
end

```

This does the correct thing in case #1, but fails in case #2.

Anyone who can help me provide a working example of a macro like this?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 7, 2022, 12:07pm UTC](https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943/2 "2022-02-07T12:07:21Z")

</div>

To get the value of `ex` you need to escape it. You will probably find this blog post useful: [jkrumbiegel.com - Julia macros for beginners](https://jkrumbiegel.com/pages/2021-06-07-macros-for-beginners/)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 7, 2022, 12:48pm UTC](https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943/3 "2022-02-07T12:48:39Z")

</div>

I think you’re looking for `@show`: [https://github.com/JuliaLang/julia/blob/e0a4b7727c2760a61c69c69c1737c46e32223d4f/base/show.jl#L1023](https://github.com/JuliaLang/julia/blob/e0a4b7727c2760a61c69c69c1737c46e32223d4f/base/show.jl#L1023)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2022, 12:57pm UTC](https://discourse.julialang.org/t/trying-to-write-a-macro-for-simple-debugging/75943/4 "2022-02-07T12:57:02Z")

</div>

> [@bcsj](#):
>
> It seems to me that I need something along the following lines. But this version doesn’t work!
> 
> ```julia
> macro db(ex::Expr)
> :(print($(string(ex)) * " = " * string(eval($ex))))
> 
> ```

Don’t call `eval` in a macro. A macro shouldn’t actually evaluate any code — you just want to rewrite it — the macro happens _before_ the code is compiled and run (so the variables don’t have values yet). i.e. you would write

```julia
macro db(ex)
    quote
        val = $(esc(ex))
        println("@db ", $(string(ex)), " = ", val)
        val
    end
end

```

giving e.g.

```julia
julia> @db 3 + 4
@db 3 + 4 = 7
7

```

This way, writing `@db 3 + 4` is _equivalent_ to writing

```julia
begin
    <somevar> = 3 + 4
    println("@db ", "3 + 4", " = ", <somevar>)
    <somevar>
end

```

where I’m writing `<somevar>` because [macro hygiene](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene) causes Julia to rename the `val` variable to a unique identifier so that it doesn’t conflict with any of your variables. This is what macros do: **they rewrite your code** , they don’t execute it.

Note that you don’t want to declare `ex::Expr`, since the argument to the macro could be e.g. a literal number instead of an expression node:

```julia
julia> @db 3
@db 3 = 3
3

```

This kind of macro, by the way, is an [exception to my rule that you should generally avoid metaprogramming](https://www.youtube.com/watch?v=mSgXWpvQEHE&t=245s), because this is something that can really only be done using macros without copy-paste code. It’s so useful that, in fact, it’s built-in as the [`@show` macro](https://docs.julialang.org/en/v1/base/base/#Base.@show) as @johnmyleswhite pointed out.
