Macro problem: Variable not defined

This works:

using BenchmarkTools
fun = () -> sqrt(3)
@benchmark fun()

This however:

benchit(f) = @benchmark f()
benchit(fun)

Results in:

ERROR: UndefVarError: f not defined

Other macros work, e.g.:

timeit(f) = @time f()
timeit(fun)

Am I missing something, or is this a problem with the @benchmark macro? Perhaps it’s not escaping the expression correctly? If I create the following two macros:

macro m1(ex) :($(esc(ex))) end
macro m2(ex) :($ex) end

The first one works when called through a function like above, while the second one gives the same ERROR: UndefVarError: f not defined.

Ah, I think I found the solution:

benchit(f) = @benchmark $f()
1 Like

Right, @benchmark operates at global scope, so any local values have to be interpolated in.