nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

`U1, U2, ...` are usable as types but not as parameters

Open IndianBoy42 opened this issue 3 years ago • 5 comments

They are defined as type Uxyz = Const::<xyz>;

When you need to pass it as a generic parameter this works.

But there are some functions (reshape_generic for example) that take Const::<N>, Const::<M> in their function arguments, as a way to avoid the turbofish, as rust can deduce the type arguments from the function arguments.

However in this position we can't use U1, U2, ...

You get an error

error[E0423]: expected value, found type alias `na::U12`

Can we have consts with the same name so Uxyz can be used as a value as well?

IndianBoy42 avatar Jan 09 '22 14:01 IndianBoy42

Doesn't this solve the problem in concern https://github.com/dimforge/nalgebra/blob/dev/src/base/dimension.rs#L281 ?

metric-space avatar Jan 09 '22 18:01 metric-space

This is something that's been bothering me too. The question is if introducing these constants can break any code - I don't think so, because the type aliases and the constant reside in type/value space respectively. I think it would be great to try this out with a PR.

Andlon avatar Jan 10 '22 08:01 Andlon

Doesn't this solve the problem in concern https://github.com/dimforge/nalgebra/blob/dev/src/base/dimension.rs#L281 ?

The problem is that you can't write e.g

matrix.reshape_generic((0, 0), (U3, U1));

because U3 and U1 are type aliases. Currently you have to write U3::name() or Const<3> instead, both of which are more verbose. Introducing constants with the same name should hopefully allow the above example to compile.

Andlon avatar Jan 10 '22 08:01 Andlon

One advantage i noticed Const::<> has is that you can put a _ in the turbo fish and the compiler can often deduce the matrix size for you

If you enable the unstable feature, but still

IndianBoy42 avatar Jan 10 '22 08:01 IndianBoy42

As investigated in https://github.com/dimforge/nalgebra/pull/1111, adding the constants could be expensive to compile and may not match the implemented ToTypenum specializations.

trueb2 avatar Jun 03 '22 13:06 trueb2