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

Macros should have an operator type #8853

Closed
darkf opened this issue Aug 29, 2013 · 4 comments
Closed

Macros should have an operator type #8853

darkf opened this issue Aug 29, 2013 · 4 comments
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@darkf
Copy link
Contributor

darkf commented Aug 29, 2013

AFAIK there is currently no way to quote an operator such as + or /, and this would be useful to reduce code duplication.

Something like:

macro_rules! foo(
    ($oper:op) => 10i $oper 20i
)

fn main() {
    printfln!("%d", foo!(+));
    printfln!("%d", foo!(*));
}
@metajack
Copy link
Contributor

This seems in the same vein as #5748

I believe all the operators map to functions in built in traits, so you can probably work around this right now by switching to the method version.

@darkf
Copy link
Contributor Author

darkf commented Aug 29, 2013

@metajack The problem with the methods is that they accept different types, for example:

fn main() {
    let x = ~"foo";
    let y = ~"bar";
    println(x.add(y));
}

/* z.rs:4:15: 4:16 error: mismatched types: expected `&&str` but found `~str` (expected &-ptr but found ~str)
z.rs:4  println(x.add(y));
                      ^
*/

@japaric
Copy link
Member

japaric commented Nov 2, 2014

This can be worked around using the "expr! trick":

#![feature(macro_rules)]

macro_rules! expr {
    ($e:expr) => {
        $e
    }
}

macro_rules! foo(
    ($op:tt) => {
        expr!(10i $op 20i)
    }
)

fn main() {
    println!("{}", foo!(+));
    println!("{}", foo!(*));
}

@nick29581 Should this be moved to rust-lang/rfcs? I recall reading somewhere that all the changes to the macro system now require an RFC. (Found it)

@rust-highfive
Copy link
Collaborator

This issue has been moved to the RFCs repo: rust-lang/rfcs#426

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
feat(fix): ignore `todo!` and `unimplemented!` in `if_same_then_else`

close: rust-lang#8836
take over:  rust-lang#8853

This PR adds  check `todo!` and `unimplemented!` in if_same_then_else.
( I thought `unimplemented` should not be checked as well as todo!.)

Thank you in advance.

changelog: ignore todo! and unimplemented! in if_same_then_else

r? `@Jarcho`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants