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

array_vec! macro cannot work with tuple element #164

Open
YoshieraHuang opened this issue Jul 8, 2022 · 1 comment
Open

array_vec! macro cannot work with tuple element #164

YoshieraHuang opened this issue Jul 8, 2022 · 1 comment

Comments

@YoshieraHuang
Copy link

This expression tries to construct an ArrayVec with element type (f32, f32) but cannot compile:

let a: ArrayVec<[(f32,f32); 3]> = array_vec!((2.0, 2.0), (2.0, 2.0), (2.0, 2.0));

The error goes like:

expected type, found `2.0`
expected type

Meanwhile, the vec! macro can handle this well.

@Lokathor
Copy link
Owner

Lokathor commented Jul 8, 2022

The problem here is that array_vec! has several ways it can be invoked.

macro_rules! array_vec {
    ($array_type:ty => $($elem:expr),* $(,)?) => { ... };
    ($array_type:ty) => { ... };
    ($($elem:expr),*) => { ... };
    ($elem:expr; $n:expr) => { ... };
    () => { ... };
}

Unfortunately, in this situation it seems to be doing a partial match on the 2nd case, ($array_type:ty) => { ... };, which then fails because that's not actually what you wanted.

I don't think there's an easy fix here, because changing the ordering of the match arms can break other people's code instead.

I'll link this issue in the discord and perhaps a macro expert can swoop in and solve the problem.

In the mean time, array_vec!([(f32,f32); 3] => (2.0, 2.0), (2.0, 2.0), (2.0, 2.0)) is more verbose but does work how you want (playground example).

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

No branches or pull requests

2 participants