# How to define a macro that evaluates in local scope

**URL:** <https://discourse.julialang.org/t/how-to-define-a-macro-that-evaluates-in-local-scope/79173>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [April 7, 2022, 2:15pm UTC](https://discourse.julialang.org/t/how-to-define-a-macro-that-evaluates-in-local-scope/79173 "2022-04-07T14:15:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpdoane](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jpdoane](https://discourse.julialang.org/u/jpdoane)\
**Post date:** [April 7, 2022, 2:15pm UTC](https://discourse.julialang.org/t/how-to-define-a-macro-that-evaluates-in-local-scope/79173/1 "2022-04-07T14:15:06Z")

</div>

I am trying to write a custom benchmarking macro, but as a first step I cannot seem to write even a trivial macro that transparently executes an arbitrary expression. Specifically if the expression is a variable assignment, the variable is not set in local scope.

Example:

```julia
macro donothing(ex::Expr)
    return ex
end

```

Returns:

```julia
julia> @donothing z=1
1

julia> z
ERROR: UndefVarError: z not defined

```

I’ve tried wrapping the expression in esc() and quote blocks to no avail. This seems really basic but I can’t seem to find an answer. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 7, 2022, 2:28pm UTC](https://discourse.julialang.org/t/how-to-define-a-macro-that-evaluates-in-local-scope/79173/2 "2022-04-07T14:28:04Z")

</div>

> [@jpdoane](#):
>
> I’ve tried wrapping the expression in esc() and quote blocks to no avail.

?

```julia
julia> macro donothing(ex::Expr)
           return esc(ex)
       end
@donothing (macro with 1 method)

julia> @donothing z = 1
1

julia> z
1

```

---

<div class="post-metadata">

**Author:** ![jpdoane](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jpdoane](https://discourse.julialang.org/u/jpdoane)\
**Post date:** [April 7, 2022, 2:32pm UTC](https://discourse.julialang.org/t/how-to-define-a-macro-that-evaluates-in-local-scope/79173/3 "2022-04-07T14:32:51Z")

</div>

Good lord, not sure what happened – I could have sworn I tried that. In any case, Thank you very much 🙂
