Tehforsch / diman

Define rust compile time unit systems using const generics
53 stars 2 forks source link

Arithmetic with references #50

Closed jedbrown closed 9 months ago

jedbrown commented 10 months ago

In various circumstances, including with iterators such as below, you're holding a reference and would like to perform arithmetic.

let velocity = [Velocity::meters_per_second(5.0); 3];
let energy = 0.5 * velocity.iter().map(|v| v * v).sum::<SpecificEnergy>();

This works in Diman if one writes the closure as |v| *v * *v, but the above works with nondimensional types and is less visually noisy. Should arithmetic (Add, Sub, Mul, Div) be implemented for all four combinations in which one or both of the args are references?

Yes, this case also works with |v| v.powi::<2>().

Tehforsch commented 9 months ago

This is a good idea. I think anything that works with floats and that we can support should be supported, so I'd like to add this.

Tehforsch commented 9 months ago

Done in #53. Your example:

let velocity = [Velocity::meters_per_second(5.0); 3];
let energy = 0.5 * velocity.iter().map(|v| v * v).sum::<SpecificEnergy>();

now compiles.

jedbrown commented 9 months ago

Wonderful, thanks!