Using splat inside macro

This isn’t really about splatting, it’s just the usual rules about macro hygiene: Metaprogramming · The Julia Language, namely that you need to escape (with esc()) values that you want to interpolate like this.

Here’s a simpler example that works. I’ve removed savename because I don’t have DrWatson, and I’ve also removed @ntuple because you can do the same thing via (; n, g) as of Julia 1.7:

function display_res(ntuple)
	println("got: ", ntuple)
end

macro display_res(vars...)
	quote
		ntuple = (; $(esc.(vars)...))
		display_res(ntuple)
	end
end

Usage:

julia> function foo()
         n = 1
         g = 2
         @display_res(n, g)
       end
foo (generic function with 1 method)

julia> foo()
got: (n = 1, g = 2)
3 Likes