-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Handle keywords in macro signatures #57233
Comments
I like the idea of adding this to the language. However, this must be attempted very carefully to not break any existing macro in the ecosystem that uses exactly such a custom parsing. |
Indeed. I think what I would do is:
I think this would match all existing semantics for packages that already manually parse keywords. For example: macro foo(args...; bar=1)
return nothing
end If you were to call: @foo x bar=2 it would route to the keyword. But if you instead call @foo x baz=3 it would get passed in as |
For some bikeshedding, it seems the dominant convention in But some functionality to handle this ergonomically (or better direction to documentation on existing functionality) seems nice. Although it's not clear there's a one-size-fits-all solution. |
Yeah, this is tricky. One thing to note is that the old way of manually writing a keyword parser would still continue to work, it is just that this approach allows for optional automatic keyword parsing should you so desire it. Because of this opt-in behavior, you could choose either:
Either choice wouldn't affect existing macros, since parsing args would still work. For 2., for example, if I write: macro code_llvm(ex; debuginfo=:default, raw=true, optimize=true)
return nothing
end Could I then call it with either |
I might lean towards handling both cases with the automatic keyword syntax. With the exception that it wouldn't handle both cases in the same macro call, nor would it permit putting any keywords in between args of the argument list. So, for macro foo(args...; kw1, kw2)
return nothing
end you could allow the following, with two expressions passed:
|
It would be great if macros could natively handle keyword parsing, such as
Many macros in the Julia ecosystem and standard library handle keywords, but do so by manually parsing the input expressions and unpacking any matching
Expr(:kw, ...)
. This results in developers handrolling their own parsers, such as@btime
:I think it would be nice if Julia itself could automatically handle this, rather than having each macro definition parse keywords in its own way. The dream is for the following syntax to work:
This would mean that developers wouldn't need to write a keyword parser in each new macro. Overall I think this would result in cleaner, more readable, more robust code.
I couldn't find an issue for this but please link if there is one. The original PR to throw errors on keywords is here: #15913. It was also requested in a comment here: #15896 (comment).
The text was updated successfully, but these errors were encountered: