Unexpected matches in @capture of function definition
I ran into some unexpected matching behavior in a function definition (see below). I'm not sure if this should be a match or not. However, if it is a match it seems to by swapping xsym and args (e.g., I would expect xsym to get :x rather than args getting that). I'm guessing this can be worked around by using some of the function definition documentation?
julia> @MacroTools.capture(:(function f(x; n=100) return 1 end), function funsym_(xsym_, args__) body_ end)
true
julia> xsym
:($(Expr(:parameters, :(n=100))))
julia> args
1-element Array{Any,1}:
:x
This is due to the fact that keyword arguments are stored as if they were the first argument to the function; when you ask for xsym_, you're really just asking for whatever happens to be in the AST in that position, so MacroTools gives you that.
One way around this would be to modify all function expressions (input and templates) so that they always have a parameters slot, even if it's empty; it's an annoying special case to have but it'd make this kind of thing behave a lot more intuitively.
Is there an open issue or PR on the main Julia repository to remove the special case with no parameters? I keep running into the same issue when trying to write & debug macros, with or without macrotools.