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