I am trying to generate modules using a macro, but it keeps failing. I am not sure how to handle this.
EG.
julia> macro mymodule(name)
quote
module ($name)
end
end
end
@mymodule (macro with 1 method)
julia> @mymodule Foo
ERROR: syntax: module expression not at top level
If you do a @macroexpand on both versions you will see that in the first instance you get something like
Expr
head: Symbol module
args: Array{Any}((3,))
1: Bool true
2: Symbol MyMod
3: Expr
head: Symbol block
args: Array{Any}((2,))
1: LineNumberNode
line: Int64 3
file: Symbol REPL[7]
2: LineNumberNode
line: Int64 3
file: Symbol REPL[7]
And in the second, you get
Expr
head: Symbol block
args: Array{Any}((2,))
1: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
2: Expr
head: Symbol module
args: Array{Any}((3,))
1: Bool true
2: Symbol MyMod
3: Expr
head: Symbol block
args: Array{Any}((2,))
1: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
2: LineNumberNode
line: Int64 3
file: Symbol REPL[12]
Those are clearly very different. Julia does not allow you to define modules inside of blocks. If it would, you can have modules defined inside for loops, which wouldn’t make much sense. As the error indicates, module declaration has to be at the top level.
The generated expression in the second case is equivalent to