# Understanding Macro

**URL:** <https://discourse.julialang.org/t/understanding-macro/18411>\
**Category:** New to Julia\
**Created:** [December 7, 2018, 8:06am UTC](https://discourse.julialang.org/t/understanding-macro/18411 "2018-12-07T08:06:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [December 7, 2018, 8:06am UTC](https://discourse.julialang.org/t/understanding-macro/18411/1 "2018-12-07T08:06:29Z")

</div>

I struggle to wrap my skull around metaprogramming!

So, following great advice (thanks guys) [[Extract intermediate data from inside a function](https://discourse.julialang.org/t/extract-intermediate-data-from-inside-a-function/18251)] I have written the following code

```julia
#module spying
#export spy
#export store
# a simple spying closure
function spy(targets)
    vals=[]
    tags=[]
    function store(val,tag)
        if tag ∈ targets
            push!(vals,copy(val))
            push!(tags,tag)
        end
    end
    store(val)=store(val,:unspecified)
    function get()
        return (vals,tags)  
    end
    return store,get
end

macro store(ex)
    @assert ex.head== :(=)
    local var = ex.args[1]
    return quote
        $ex
        store($var,var)
    end
end
#end #module spying

#using spying
# a function to spy on
f(x) = f((val,tag)->nothing,x)
function f(store,x)
    y = x
    for i = 1:3
        @store y = 3*y
        #y = 3*y
        #store(y,:y)
    end
    z = y+2
    store(z,:z)
    return 2z
end

(store,results)=spy([:y,:z])
f(store,3)
(vals,tags)=results()

display(vals)
display(tags)

```

The idea is to be able to extract intermediate results from function f, by spending in a function to retrieve the data. Any way, if, int he definition of f I comment the line with a macro, and uncomment the two following lines, everything works like a breeze. But my “macro store” is not doing what I want.

a) Executing the code generated by the call `@store y = 3*y` generates a “y not defined” error. But the code `@store y = 3` does not generate this error.  
b) I want the macro to generate the code `store(y,:y)` and I do not know how to generate `:y`

: )

Philippe

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 7, 2018, 8:22am UTC](https://discourse.julialang.org/t/understanding-macro/18411/2 "2018-12-07T08:22:05Z")

</div>

I think this is what you want:

```julia
julia> macro store(ex)
           @assert ex.head== :(=)
           local var = ex.args[1]
           return quote
               $(esc(ex))
               store($(esc(var)), $(Meta.quot(var)))
           end
       end
@store (macro with 1 method)

julia> @macroexpand @store y = 3*y
quote
    #= REPL[18]:5 =#
    y = 3y
    #= REPL[18]:6 =#
    (Main.store)(y, :y)
end

```

You need to escape the `ex`, otherwise the `y` gets mangled. (Although I’m a bit surprised that the mangled name does not show up in the error).

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [December 7, 2018, 8:25am UTC](https://discourse.julialang.org/t/understanding-macro/18411/3 "2018-12-07T08:25:46Z")

</div>

BINGO, you answered both questions. The “esc”, I should have known. The Meta.quot: well that was exotic!

😀 Thank you!
