Skip to content

Macros should have an operator type #426

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

Open
rust-highfive opened this issue Nov 2, 2014 · 7 comments
Open

Macros should have an operator type #426

rust-highfive opened this issue Nov 2, 2014 · 7 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@rust-highfive
Copy link

Issue by darkf
Thursday Aug 29, 2013 at 11:55 GMT

For earlier discussion, see rust-lang/rust#8853

This issue was labelled with: A-syntaxext, I-enhancement in the Rust repository


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!(*));
}
@brendanzab
Copy link
Member

Might be better to call it binop...? At the moment I just use the operator trait method names...

@petrochenkov petrochenkov added the T-lang Relevant to the language team, which will review and decide on the RFC. label Jan 19, 2018
@saskenuba
Copy link

+1, this is really useful and would avoid a lot of boilerplate especially in operator overloading on simple structs

@petrochenkov
Copy link
Contributor

tt can be used for passing a single token.

macro_rules! foo {
    ($oper:tt) => (10 $oper 20)
}

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

It won't reject non-operators though.

@glittershark
Copy link

tt can be used for passing a single token.

That doesn't work in all places an operator can exist, unfortunately, since eg expr can't be followed by tt:

macro_rules! example {
    ($lhs:expr $op:tt $rhs:expr) => {}
}
error: `$lhs:expr` is followed by `$op:tt`, which is not allowed for `expr` fragments
 --> src/main.rs:7:16
  |
7 |     ($lhs:expr $op:tt $rhs:expr) => {}
  |                ^^^^^^ not allowed after `expr` fragments
  |
  = note: allowed there are: `=>`, `,` or `;`

@kennytm
Copy link
Member

kennytm commented Sep 27, 2021

$:expr can't be followed by a +, let alone any binary operator.

error: `$lhs:expr` is followed by `+`, which is not allowed for `expr` fragments
 --> src/lib.rs:2:16
  |
2 |     ($lhs:expr + $rhs:expr) => {}
  |                ^ not allowed after `expr` fragments
  |
  = note: allowed there are: `=>`, `,` or `;`

@glittershark
Copy link

oh wow, ok. Probably a separate rfc for that, in that case

@kennytm
Copy link
Member

kennytm commented Sep 27, 2021

there is no way to "fix" this because example!(1 + 2 + 3 + 4) is a thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants