Open rursprung opened 11 months ago
Rust as a language doesn't have const trait
implementations yet, so we don't have any way to make stuff like operator +
const. Most of the inherent methods of Complex
also depend on trait bounds, so they can't be const yet either -- e.g. even something simple like const fn i()
would need const Zero
and One
.
oh, sorry, i didn't even realise that!
i just looked around a bit and it seems that it'll need this nightly feature to be stabilised: https://github.com/rust-lang/rust/issues/67792
once that's done it should be possible to make these implementations const
For simple stuff like Add
, at least it's not too bad to do it manually:
const X: Complex32 = Complex32::new(1., 0.);
const Y: Complex32 = Complex32::new(0., 1.);
const Z: Complex32 = Complex32::new(X.re + Y.re, X.im + Y.im);
But of course this would be a pain for more complex expressions. (pun intended :smile:)
If you have a lot of constant values like this that you want to prepare, you could also do it in a build.rs
script to write a generated source file, as described in the Cargo book:
https://doc.rust-lang.org/cargo/reference/build-script-examples.html#code-generation
as the title says: it'd be great if as many functions as possible were made
const
so that they can be used in a const environment.currently this code will not compile because the
Add
implementation ofComplex
isn'tconst
:you can find the same example on the playground.
error from the playground: