# Is there a way to determine whether code is toplevel?

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939
**Category:** General Usage
**Created:** [May 22, 2020, 7:32am UTC](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939 "2020-05-22T07:32:00Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [May 22, 2020, 11:47pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939/3 "2020-05-22T23:47:57Z")

</div>

Ha! That works, thanks.

```julia
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.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939)._
