How to define a macro that evaluates in local scope

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:

macro donothing(ex::Expr)
    return ex
end

Returns:

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!

?

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

julia> @donothing z = 1
1

julia> z
1
1 Like

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

1 Like