# Global struct defined in some local scopes / what \*is\* top level?

**URL:** <https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943>\
**Category:** General Usage\
**Tags:** scope, struct\
**Created:** [July 28, 2022, 10:08pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943 "2022-07-28T22:08:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 28, 2022, 10:08pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/1 "2022-07-28T22:08:58Z")

</div>

Struct definitions nested in macros or methods throw an error explaining they need to be at top level. Top level is generally explained as the module’s global scope, but it’s evidently not (example below). Furthermore, the struct names escape the local scope unlike other local variables, behaving as if they were defined in the global scope. Does anyone know a rigorous definition of top level that takes into account how it dips into some local scopes (afaik: let, for, while, try, struct)?

```julia
julia> let
        struct X end
        x = X()
       end
X()

julia> X
X

julia> x
ERROR: UndefVarError: x not defined

julia> for i in 1
        struct Y end
        y = Y()
       end

julia> Y
Y

julia> y
ERROR: UndefVarError: y not defined

julia> macro blah()
        struct X end
        1
       end
ERROR: syntax: "struct" expression not at top level
Stacktrace:
 [1] top-level scope
   @ REPL[11]:1

julia> function blah()
        struct X end
        1
       end
ERROR: syntax: "struct" expression not at top level
Stacktrace:
 [1] top-level scope
   @ REPL[12]:1

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 28, 2022, 10:29pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/2 "2022-07-28T22:29:31Z")

</div>

1. `let` always create hard local scope
2. function and type definition always evaluated at (module) top level

combined these two you get what you see here

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 28, 2022, 10:31pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/3 "2022-07-28T22:31:22Z")

</div>

> [@jling](#):
>
> function and type definition always evaluated at (module) top level

except for nested method definitions, perhaps

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 28, 2022, 10:31pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/4 "2022-07-28T22:31:52Z")

</div>

you mean closure?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 28, 2022, 10:41pm UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/5 "2022-07-28T22:41:52Z")

</div>

Yes, semantically at least. I suppose the nested method is actually implemented as a functor in the local scope while the method with some modifications is hoisted to top level.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [July 29, 2022, 11:18am UTC](https://discourse.julialang.org/t/global-struct-defined-in-some-local-scopes-what-is-top-level/84943/6 "2022-07-29T11:18:03Z")

</div>

The above comments don’t seem very helpful to your original question.

Fwiw, I’m surprised too. For example, in the case of the struct defined in the let scope, it’s still surprising to me that it’s definition escapes the scope of this block. Clearly top level and module scope mean different things, and until now I’d assumed them to be the same.
