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
Interpolate non-repeating variable inside iterating pattern #4
Comments
Two alternatives:
|
After a lot of thought and many dead ends I think I'm finally getting on to something. The current progress to solve this issue is in the The main change here is the introduction of the What does
What does NOT
Problem with the current implementationHowever, I'm having trouble fixing name collision. While iterators have to be consumed to be used (transfer ownership), slices and Two non-ideal solutions
let a = vec!["a", "b", "c"];
let b = [5,6,7];
let c = 5;
let d = "a";
let e = X;
let a = &a;
let b = &b;
let c = &c;
let d = &d;
let e = &e;
let q = quote!{ #(#a #b #c #d #e)* }; I'm more inclined to the first solution because it would have less practical impact on the user. But I would really like to find a better way to fix this. |
@dtolnay you have much experience with macros, so I'd like to know if some trick comes to your mind that would allow to fix this problem, such as a way to call |
Opened #7 for the name collision issue. |
Nothing comes to mind to solve either the name collision or the inferring when a variable needs to be taken as a reference. But I think your method name is unique enough to not worry about collisions. |
There is a problem here, AFAICT something like this will repeat ad infinitum without warning (and end up allocating GBs of RAM until stopped by the OOM killer): let x = quote!(_);
quote(#(#x)*); If |
This issue tracks the follow up of dtolnay/quote#7.
My concern about this feature is that it would make interpolation of types that implement both
ToTokens
andIterator<Item=ToTokens>
ambiguous. (example:TokenStream
, a type that will probably come up alot in interpolations).In fact, I think the most common use for
TokenStreams
would be to be used as aToTokens
rather than an iterator.As a rough made up example:
As
TokenStream
implementsIterator<Item=TokenTree>
, this would (understandably) give a result that wasn't expected from the user. I fear this could become confusing in much more complex macros.By not allowing non-repeating variables altogether, users would be forced to explicitly turn every item they want to use as
ToTokens
before passing them to the quoteThis case is not ambiguous and less error prone, as everything inside
#(...)*
must be an iterator and the variables that would intended to be used asToTokens
are explicitly stated as so with thestd::iter::repeat_with
before thequote!
.The text was updated successfully, but these errors were encountered: