Consider a macro that defines a simple struct:
julia> macro bar()
Expr(:struct, true, :Bar, Expr(:block, Expr(:(::), :field, :Int)))
end
@bar (macro with 1 method)
julia> @macroexpand @bar
:(mutable struct Bar
field::Main.Int
end)
julia> @bar
julia> Bar(4)
Bar(4)
No problem.
Mark the field as const and the hygiene pass starts believing field is a variable instead of a field:
julia> macro foo()
Expr(:struct, true, :Foo, Expr(:block, Expr(:const, Expr(:(::), :field, :Int))))
end
@foo (macro with 1 method)
julia> @macroexpand @foo
:(mutable struct Foo
const Main.field::Main.Int
end)
resulting in the confusing error message:
julia> @foo
ERROR: syntax: expected assignment after "const" around REPL[7]:1
Stacktrace:
[1] top-level scope
@ REPL[7]:1
Of course a solution consists in wrapping :field in an esc call, but that shouldn't be necessary (since it's not necessary in the first case).
Consider a macro that defines a simple struct:
No problem.
Mark the field as
constand the hygiene pass starts believingfieldis a variable instead of a field:resulting in the confusing error message:
Of course a solution consists in wrapping
:fieldin anesccall, but that shouldn't be necessary (since it's not necessary in the first case).