# 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:** 4
**Page:** 1

<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, 7:32am UTC](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939/1 "2020-05-22T07:32:00Z")

</div>

I’m looking for a hypothetical `@istoplevel`, presumably a macro, which could work like this:

```julia
module Foo
@istoplevel() # true
end

```

```julia
julia> @istoplevel() # true

```

```julia
function foo()
    @istoplevel() # false
end

```

Does anything like this exist or can it be constructed?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [May 22, 2020, 3:25pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939/2 "2020-05-22T15:25:29Z")

</div>

Yes, you can set a `gensym` variable and test if it is indeed set on ` __module__ `.

---

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

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [November 28, 2025, 3:08pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939/4 "2025-11-28T15:08:56Z")

</div>

Sorry for necroposting. Just be aware that this detects ‘toplevel [scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-scope-table)’, but not ‘toplevel’ in the sense that _“a module may be defined here”_.  
In particular:

```julia
println(@istoplevel) # true

let
  println(@istoplevel) # false
end

# but..
begin
  println(@istoplevel) # true
end

# and..
if true
   println(@istoplevel) # true
end

```
