Skip to content

Macro to run a macro over each tuple type #24830

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

Closed
joshtriplett opened this issue Apr 26, 2015 · 3 comments
Closed

Macro to run a macro over each tuple type #24830

joshtriplett opened this issue Apr 26, 2015 · 3 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@joshtriplett
Copy link
Member

In the course of implementing the Bounded trait for tuples (rust-num/num#89), I implemented a generic macro that runs the macro passed as its argument over every tuple type up to 20:

macro_rules! for_each_tuple_ {
 ( $m:ident !! ) => (
 $m! { }
 );
 ( $m:ident !! $h:ident, $($t:ident,)* ) => (
 $m! { $h $($t)* }
 for_each_tuple_! { $m !! $($t,)* }
 );
}
macro_rules! for_each_tuple {
 ( $m:ident ) => (
 for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
 );
}

There are other instances of this pattern in Rust libraries; for instance, see

macro_rules! impl_hash_tuple {
and
impl_hash_tuple! {}
. The latter could be replaced with for_each_tuple!(impl_hash_tuple);

Would it make sense to put something like this somewhere in the Rust standard libraries, marked as unstable since it depends on defining macros? This would be useful for anyone implementing traits on tuples.

@soltanmm
Copy link

This seems related to the future possibility of variadic generics in terms of application.

@steveklabnik steveklabnik added A-libs C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Apr 28, 2015
@eddyb
Copy link
Member

eddyb commented May 6, 2015

@pczarn was working on "higher-order macros" at some point. If implemented, it would allow you to pass the body of a macro to another macro, as you would pass a closure to a function.

It would turn http://is.gd/XQOy1R into http://is.gd/PyRjlK, but you can see that's not working because the expanded macro body is treated differently there than it is in a regular definition using macro_rules!.

@steveklabnik
Copy link
Member

Macro additions are really huge, because they can't be individually imported. Adding macros to the standard library is a really big deal, so I'm moving this issue to the RFCs repo: rust-lang/rfcs#1365

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

No branches or pull requests

4 participants