nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

`const fn` constructors

Open whatisaphone opened this issue 6 years ago • 8 comments

const fn has landed in Rust stable! It would be really helpful in one of my projects if nalgebra adopted it for constructors. The shortlist of types I need is Point2, Point3, Vector2, Vector3, UnitComplex, UnitQuaternion. But it probably makes sense for every type in the library.

Things like UnitComplex::new might not work due to compile-time restrictions on FP math, but I'm happy to work around that with UnitComplex::from_cos_sin_unchecked or whatever else makes sense.

Adding the keyword is a simple enough change (at least I hope!), but it will break on older compilers and I don't know your policy on Rust version compatibility.

whatisaphone avatar Jan 06 '19 16:01 whatisaphone

I don't believe this is possible quite yet! The current level of const_fn support doesn't include functions with trait bounds. Not only do we need this feature to mark constructors as const in nalgebra (because of the pervasive N: Scalar, R: Dim, C: Dim, S: Storage bounds), but we need this feature to make the constructors in generic-array const, too.

jswrenn avatar Jan 07 '19 20:01 jswrenn

Aw, you're right :(

I guess this becomes the tracking issue then.

In the meantime I just learned about the union transmute hack, so that's the alternative for now.

related https://github.com/rust-lang/rust/issues/24111

whatisaphone avatar Jan 07 '19 21:01 whatisaphone

Revisiting this issue to see if the situation improved since last year. It appears that const fn on functions with trait bounds is still a problem:

error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
    --> src/base/construction.rs:842:14
     |
842  |           impl<N> MatrixMN<N, $R, $C>
     |                ^
...

sebcrozet avatar Jun 10 '20 19:06 sebcrozet

Side note, it would be nice if we could have a feature to enable these on nightly even though it'd be a mega pain to get working with macros.

clarfonthey avatar Oct 12 '20 01:10 clarfonthey

I'm pretty sure trait bounds on const fns are now stable. It would be nice if we could revisit this.

Edit: confirmed stable since 1.61.0, just about two years ago.

lylythechosenone avatar May 16 '24 02:05 lylythechosenone

Calling trait methods in const isn't, so I'm not sure you'll be able to constify any useful constructors.

Ralith avatar May 16 '24 04:05 Ralith

The point!, vector! and matrix! construction macros are all usable in a const context, allowing you to construct const vectors, matrices and points.

The Unit-related constructors are probably currently not possible to support in const though, since AFAIK you'll need methods from SimdRealField or similar traits.

Andlon avatar May 16 '24 07:05 Andlon

Ah, in that case this is not supported yet. Indeed, you still cannot call trait methods in const (coming soon, hopefully).

lylythechosenone avatar May 17 '24 12:05 lylythechosenone