Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected matches in @capture of function definition #120

Open
omalled opened this issue Aug 16, 2019 · 2 comments
Open

Unexpected matches in @capture of function definition #120

omalled opened this issue Aug 16, 2019 · 2 comments

Comments

@omalled
Copy link

omalled commented Aug 16, 2019

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
@MikeInnes
Copy link
Member

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.

@saolof
Copy link

saolof commented Oct 10, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants