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

Add syntax extension ary![expr, ..N] #14272

Closed
wants to merge 1 commit into from

Conversation

lilyball
Copy link
Contributor

ary![expr, ..N] expands to an array by repeating expr N times. This is
in contrast to [expr, ..N] which Copies expr N times.

Sample usage:

let x = ary![None::<Vec<u8>>, ..10];

The one limitation is the count needs to be an integral literal, it
cannot be a static or a compile-time expression.

Related to #5244, but is more flexible.

ary![expr, ..N] expands to an array by repeating `expr` N times. This is
in contrast to [expr, ..N] which Copies `expr` N times.

Sample usage:

    let x = ary![None::<Vec<u8>>, ..10];

The one limitation is the count needs to be an integral literal, it
cannot be a static or a compile-time expression.
@alexcrichton
Copy link
Member

If this is added, I do not think it should be part of the public API of the compiler. It should either be behind a feature gate or in a separate library because in the ideal world this macro is not as necessary with proper checking for expressions that can be repeated.

Our rules for what can be in a static are basically the same for what can be a repeated expression, we just haven't applied the same logic to these expressions.

@lilyball
Copy link
Contributor Author

in the ideal world this macro is not as necessary with proper checking for expressions that can be repeated

True, and that reminds me, I meant to mention the related issues. Although even fixing that won't remove the need for ary![], as things like ary![Vec::new(), ..64] are still useful.

I'm open to feature gating if necessary. Presumably a decision would be made about it after the relevant tickets have been fixed and we can better evaluate how useful this is. Putting it in a separate library might be a bit much, I feel like saying #[feature(phase)] #[phase(syntax)] extern crate ary; is a lot of boilerplate if I just need ary![] once or twice.

}
Some(x) => x
};
let exprs = Vec::from_fn(count, |_| val_expr.clone());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This references the same Expr repeatedly in the vector. Is this ok, or should I be calling .duplicate() on it?

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with a rebase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants