# Generated Code needs 2 scopes! module level scope and current scope

**URL:** https://discourse.julialang.org/t/generated-code-needs-2-scopes-module-level-scope-and-current-scope/27125
**Category:** General Usage
**Created:** [August 3, 2019, 4:00am UTC](https://discourse.julialang.org/t/generated-code-needs-2-scopes-module-level-scope-and-current-scope/27125 "2019-08-03T04:00:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [August 3, 2019, 4:00am UTC](https://discourse.julialang.org/t/generated-code-needs-2-scopes-module-level-scope-and-current-scope/27125/1 "2019-08-03T04:00:12Z")

</div>

```julia
macro instantiate(typ)
           quote
               struct $typ
               end
               $typ()
           end |> esc
 end

tt = @instantiate(TT) # this is ok

julia> f(x) = begin
           @instantiate NN
       end
ERROR: syntax: "struct" expression not at top level

```

is there an expression which allows struct to be evaluated at top level, the other part in the function scope  
so that this works out?

THanks

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 3, 2019, 10:18am UTC](https://discourse.julialang.org/t/generated-code-needs-2-scopes-module-level-scope-and-current-scope/27125/2 "2019-08-03T10:18:16Z")

</div>

What do you want to do? You cannot define a struct local to a method. If you want to define a struct in the module from a method you can use eval.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [August 4, 2019, 2:13am UTC](https://discourse.julialang.org/t/generated-code-needs-2-scopes-module-level-scope-and-current-scope/27125/3 "2019-08-04T02:13:15Z")

</div>

Yeah, this would be a good feature. Lowering has it internally (called toplevel-but-first, iirc) so I think we should expose that for uses like this.
