Simple question about macro arguments

I forgot to declare the variables before the let. There was also a typo. This one should work:

macro compute_∇P(li, lj)
    quote
        ∂xPx, ∂zPx, ∂xPz, ∂zPz = let li = $li, lj = $lj
            lip, lim = li+1, li-1
            ljp, ljm = lj+1, lj-1
            ∂xPx = (P_shared[lip, lj, 1] - P_shared[lim, lj, 1])*0.5/Δ
            ∂zPx = (P_shared[li, ljp, 1] - P_shared[li, ljm, 1])*0.5/Δ
            ∂xPz = (P_shared[lip, lj, 2] - P_shared[lim, lj, 2])*0.5/Δ
            ∂zPz = (P_shared[li, ljp, 2] - P_shared[li, ljm, 2])*0.5/Δ
            ∂xPx, ∂zPx, ∂xPz, ∂zPz
        end
    end |> esc
end

I’m using let to avoid overwriting existing variables li and lj. This may not be a concern in your case. A different way to achieve this would be to use gensym to create new variable names.

1 Like