dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
1.02k stars 56 forks source link

Wishlist: local token identifiers? #81

Closed jsgf closed 1 year ago

jsgf commented 2 years ago

I have some uses of paste where the same set of token pastes are repeated a few times. For example, within a macro_rules:

paste::paste! {
    struct [<$name:camel Struct>] {
      // ...
    }

    impl [<$name:camel Struct>] {
       // ...
    }

    impl SomeTrait for [<$name:camel Struct>] { ... }
}

It would be nice to have a mechanism to locally define a symbol representing the [<$name:camel Struct>] token pasting expression. I could add a new layer of macro expansion to capture that expression, but that doesn't feel like a great solution.

Is there some other existing idiom for this?

If not, then as a total straw-man suggestion for syntax, maybe something like:

paste::paste! {
    paste::define!(structname, [<$name:camel Struct>]);

    struct $structname {
    ...
banool commented 2 years ago

Hey @jsgf! Chiming in to say I'd love this as well, I find myself repeating the same thing a lot.

dtolnay commented 1 year ago

I think I would prefer not to support this in this crate. But anybody else should feel free to experiment with a more fully featured crate that can do this kind of thing.