-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[WIP] Add implicit named arguments for fmt macros #65338
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
r? @petrochenkov (hygiene & stuff...) |
I love the idea (at least in the single identifier variation), and it proved itself quite useful in struct literal expressions like It's still think it may be a noticeable enough addition to deserve an RFC. |
The hygiene question is pretty simple here - inherit the identifier span from the string literal (at least the hygienic context part of it, the location can probably be made more precise). |
Most of built-in macros are basically library proc macros now, so they should be under T-libs rather than T-lang? |
Cool! It took me a while to realise what you meant by the shorthand being very similar in struct literal expressions. Yeah I guess it is (and funnily also a shorthand available in Javascript!) 😄
Yep, I think I see what I need to do to narrow down the location but would need a bit more change to the code than what I've done so far, so will wait until we know exactly what's agreed upon.
👍 |
This seems like a great enhancement to me! I think this is something that would need an RFC. That would also be a chance for libraries maintaining their own macros that mimic |
We discussed this in the lang team meeting. In general, we are enthusiastic about this idea. We all have that experience that most of the time we use |
\o/ I'll get to work on an RFC at the weekend! |
Closing in favor of the upcoming RFC. |
It struck me the other day that it would be really nice to omit the named argument to
println!
for lots of simple use cases, e.g.:I believe adding this shorthand will make a lot of invocations to the
format
family of macros marginally shorter, and slightly easier to read. There's also a lot of precedent for it in high-level languages e.g. Javascript backticks, Python f-strings.I actually opened a thread about this on internals a while ago: https://internals.rust-lang.org/t/println-use-named-arguments-from-scope/10633
The response on the thread was that it generally seems reasonable and ergonomic to extend these macros to implicitly use simple identifiers. We didn't think it was desirable to support arbitrary expressions. So I've gone ahead and implemented this little PR to write a test implementation. If this needs an RFC I can backtrack and write one.
If the concensus is that this is a nice addition to the macros, then I'll need to do a little more work:
std::fmt
module to cover this new functionalityrecipient
in this scope". In this simple first pass of the PR the error span is the whole format string for the macro. It really should be just underneath the invalid identifier.println!("Hello, {self.recipient}")
, which would be even more flexible though could occasionally have side effects from dereferencing.Hope you guys like the idea 😄