Is there a way to determine whether code is toplevel?

Ha! That works, thanks.

macro istoplevel()
    canary = gensym("canary")
    quote
        $(esc(canary)) = true
        Base.isdefined($__module__, $(QuoteNode(canary)))
    end
end

I think that might be OK for me, but would still be interested in if there’s a more direct solution.

3 Likes