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

No way to have + or * as separator token in macros #18700

Open
bouk opened this issue Nov 6, 2014 · 6 comments
Open

No way to have + or * as separator token in macros #18700

bouk opened this issue Nov 6, 2014 · 6 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@bouk
Copy link
Contributor

bouk commented Nov 6, 2014

I'm trying to write a macro that sums a list of expressions, but it doesn't work because it's not possible to have + or * as the seperator token. For example:

macro_rules! sum {
    (
        $($e:expr)+ => (
        $($e)++ // ???
    )
}

It'll interpret is as there being no seperator token, and it seems to just stick a + at the end. Escaping the first + doesn't work either.

I'm using rust head

@bstrie

@steveklabnik steveklabnik added O-macos Operating system: macOS A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) and removed O-macos Operating system: macOS labels Jan 27, 2015
@steveklabnik steveklabnik added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 2, 2016
@steveklabnik
Copy link
Member

Triage: no change. Not sure this is going to be implemented with the current macro system.

@Gankra
Copy link
Contributor

Gankra commented Feb 2, 2016

@nrc will macros 3.0 or whatever potentially enable this?

@nrc
Copy link
Member

nrc commented Feb 3, 2016

It's not part of my plans, but it seems like something we could address separately (once the new system is in place I mean, not in the old system).

@apasel422 apasel422 changed the title No way to have + or * as seperator token in macros No way to have + or * as separator token in macros May 26, 2016
@steveklabnik
Copy link
Member

Triage: no change

@AlphaModder
Copy link

There's a workaround for this, just use the following utility macro:

macro_rules! strip_plus {
    {+ $($rest:tt)* } => { $($rest)* }
}

Then, in your main macro instead of writing $($e)++, write strip_plus!($(+ $e)+). Obviously a slight modification makes this work for * too.

@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 12, 2020
@WaffleLapkin
Copy link
Member

There is a similar problem with separators in patterns:

macro_rules! example {
    ( $( $i:ident )+* ) => {}
}

This is allowed, however it works as 'at least one ident and a * token at the end', and not as '0 or more ident separated by + token'.

There is also a workaround:

macro_rules! workaround {
    ( $( $first:ident $( + $rest:ident )* )? ) => {}
}

But it makes the macro a lot less readable (and forces you to repeat the pattern twice).

Are there any plans to do something with this issue? (maybe reopen rust-lang/rfcs#944 ?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants