4 ms·
Thanks to generics you could theoretically work with whatever underlying type makes the most sense for your use-case. let v1 : Inches<Ratio> = Ratio::new(5
by stouset 2y ago
Thanks to generics you could theoretically work with whatever underlying type makes the most sense for your use-case.
let v1 : Inches<Ratio> = Ratio::new(5, 8).into();
let v2 : Inches<Ratio> = Ratio::new(3, 8).into();
let v3 : Feet<_> = (v1 + v2).into();
assert_equal!(v3, Ratio::new(1, 12));
- throwawaymaths 2y agono that's not the point. if you let rust automagically decide when and where to apply conversions you could easily wind up in a situation where you have more operations than you need, which increases numerical error, and also be a bitch to uncover or refactor to minimize conversions.
- stouset 2y agoRust doesn’t automatically apply conversions, full stop. And with a thoughtful approach to the API, you could avoid numerical error entirely by using integral types.
- amenghra 2y agoThat’s a good starting point. The temperature delta issue remains: adding and subtracting temperatures should create a different unit. Thankfully there aren’t a lot of non-linear units.
- throwawaymaths 2y agothats why i put the word "you" in there. rust will do whatever automagic conversions you specify in the impls. Perhaps i should have used the verb "make" instead of "let". if you make rust automagically convert.
- deleted 2y ago[deleted]