-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Self-calling functions; usecase: macros #15854
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
Comments
(Posts and comments with This behaviour is not similar to the C #![feature(macro_rules)]
fn returns_unit() { }
macro_rules! function( () => { returns_unit() } )
fn main() {
function!()
function!()
function!()
} (But The only reason the
Macros are proper AST items, and cannot expand to things like a half statement or a partial declaration, e.g. a macro cannot expand to just macro_rules! if_true { () => { if true {} } }
// try to merge the `else` into the `if
if_true!() else {} i.e. the contents of a macro are truly contained (other than any actual item definitions they introduce, but theoretically hygiene means these are also implied by each invocation). I think this semicolon behaviour is more to give a form of consistency for macros that define things (One doesn't have to put fn main() {
macro_rules! foo {
(...) => { ... }
}
if true { foo!(1, 2) } else { foo!(3, 4); bar() }
} In any case. I fail to see how a self invoking function is any nicer than just expanding to a normal |
My intention was so that e.g. here we have:
This is a function call, so it must be followed by a semicolon, the same way as I'm just learning Rust, so I don't know well how macros work yet, but I hope that my intention is clear. Does it make sense? |
As I said above, whether a So it sounds a little like your proposal also requires changing the semicolon rules, to be based on the actual AST structures to which the macro expands. (Rather than just imposing the type requirement of the expanded result, as is currently done.) Also, in Rust, an anonymous functions don't really work like that:
In JS, all functions are closures implicitly, and neither of 2 or 3 apply: there's no language-level alternative to the dynamic dispatch (and Rust is aiming at a space where always-dynamic is not justifiable) and there's no concept of by-value/by-reference or moving of variables. |
Thank you for the response. I don't think I have enough background to continue the discussion. I was just bothered by the inconsistency of functions vs macros, and tried to propose a solution without actually understanding how things work. If you think that my suggestion is invalid, feel free to close the issue. |
@justanotheranonymoususer You raise a valid concern, usability for beginners. Having semicolons sometimes and sometimes not is strange. Even if it is technically correct, it is still ergonomically weird. |
Given that this kind of change would require an RFC, and it's not likely to be changed without serious work on said RFC, I think we should just close this issue. |
I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized. This issue has been moved to the RFCs repo: rust-lang/rfcs#728 |
…lnicola fix: Ignore doc(hidden) attr if no body is present fixes rust-lang#15782
I've created a discussion topic on Reddit, but for some reason it disappeared from the Rust page, so I'm filling an issue here. If there's a better place to discuss similar suggestions, please tell me.
I was just playing with Rust on http://rustbyexample.com/, and I've accidentally discovered that writeln can be called without a semicolon.
It doesn't look like a big deal, but for a perfectionist like me, I see it as a consistency flaw, especially for (probably) the most common macro in the language.
The author of Rust by example explained what's going on here, but that doesn't look right to me. Even C has a do-while(0) hack for this case.
This is an expanded
println!("test")
. Thematch
looks very similar to the do-while(0) C hack for macros, and I think that it's the wrong way to do it, not only because it looks and feels like a hack, but also because it behaves differently than a native Rust function. For example,println!("test") println!("test")
compiles, whilereturns_unit() returns_unit()
doesn't.My proposal: use a self-calling function, similar to what JavaScript allows. I don't know whether it's currently possible in Rust, but if not, I think it could be a natural addition to the language. Here's how I expect it to look, more or less.
The text was updated successfully, but these errors were encountered: