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

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

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.

5 Likes

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.

1 Like