Hacker News new | past | comments | ask | show | jobs | submit login

Might be annoying if you’re working with floating point but given that each conversion introduces error, it’s probably good to be explicit and recommend internally to be consistent





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));

no 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.

Rust 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.


That’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.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: