paholg / dimensioned

Compile-time dimensional analysis for various unit systems using Rust's type system.
https://crates.io/crates/dimensioned
MIT License
300 stars 23 forks source link

has_units() -> bool #82

Closed MarkSwanson closed 2 years ago

MarkSwanson commented 2 years ago

I can use your (paholg) suggestion to divide by 1 using the expected units, then visually inspect the formatted String to see if any mks values are present. If they are, then the units were unexpected and I know I have a problem with my math. Thanks dimensioned!

But... I believe this doesn't scale. Is it possible to get a public method to determine if the Dimensioned value is unitless? Then I wouldn't have to visually inspect things to make sure only my expected type is there.

Alternatively I could format the Dimensioned value to a string and then parse the string to see if other units are there.

I'm just nervous about having to perform a mental visual check on all results.

Thoughts?

paholg commented 2 years ago

If the quantity is unitless, it will have a value() method, so you can do

(x / Joule::new(1.0)).value()

which will only compile if x is in Joules.

MarkSwanson commented 2 years ago

sneaky :-D I love it. Thanks!