# How to know if top-level

**URL:** <https://discourse.julialang.org/t/how-to-know-if-top-level/79234>\
**Category:** General Usage\
**Created:** [April 8, 2022, 6:03pm UTC](https://discourse.julialang.org/t/how-to-know-if-top-level/79234 "2022-04-08T18:03:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 8, 2022, 6:03pm UTC](https://discourse.julialang.org/t/how-to-know-if-top-level/79234/1 "2022-04-08T18:03:24Z")

</div>

My macro expands into `... export foo`, which causes a

```julia
ERROR: syntax: "export" expression not at top level

```

I can put it in a `try/catch` block, but is there an `@is_top_level()` magic macro that I could use instead?

---

<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:** [April 8, 2022, 6:06pm UTC](https://discourse.julialang.org/t/how-to-know-if-top-level/79234/2 "2022-04-08T18:06:59Z")

</div>

I wish there was something better than this kludge but afaik there’s not: [Is there a way to determine whether code is toplevel? - #3 by marius311](https://discourse.julialang.org/t/is-there-a-way-to-determine-whether-code-is-toplevel/39939/3)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [April 8, 2022, 6:10pm UTC](https://discourse.julialang.org/t/how-to-know-if-top-level/79234/3 "2022-04-08T18:10:30Z")

</div>

That’s nice, but won’t help here, because `export` is a syntax error. It can’t be in the macroexpansion. In fact,

> I can put it in a `try/catch` block,

is wrong, dunno what I was thinking. Can’t exception-handle out of a syntax error…

---

<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:** [April 8, 2022, 6:24pm UTC](https://discourse.julialang.org/t/how-to-know-if-top-level/79234/4 "2022-04-08T18:24:44Z")

</div>

I suppose maybe `@is_top_level() && @eval export Foo` ?
