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

Units might seem simple but they have a ton of edge cases. Do you want to be able to add inches and feet? Be careful about potential precision/rounding issues. What is the unit for a temperature delta? You can’t simply keep the original unit (eg C or F) because conversion from F to C is a different rule than ΔF to ΔC. Etc.

Units do prevent bugs in programs, so they have an important role to play. But they also need to be designed very carefully.

Java adopted units via JSR 385 (https://belief-driven-design.com/java-measurement-jsr-385-21...)




> Do you want to be able to add inches and feet?

This probably doesn't have to be too complicated; the usual answer for Rust is "no". Rust doesn't even let you "just" add two unsigned integers of different sizes. Following that design, I would imagine units would require an explicit "turn feet into inches" or the other way around.


That's where Scala shines. I wrote about this here a bit: https://valentin.willscher.de/posts/contextual-syntax/

Rust is heavily inspired by Scala, but I guess achieving something like the examples in my post is difficult. I really hope Rust finds one way or another to make it work. Because simply forbidding everything all the time isn't even the safest way - it drives many people to just avoid it altogether and use unsafe code.


While at it, could you give a few examples that illustrate how Rust was inspired by Scala proper, and not Haskell / SML / OCaml (which also influenced Scala)?


This page from official Rust website of its influences does not mention Scala. https://doc.rust-lang.org/reference/influences.html

In fact this is the first time I've seen anyone say Scala influenced Rust let alone "heavily". Seems like a stretch


It says here:

https://en.wikipedia.org/wiki/ML_(programming_language)

that ML influenced Rust, Scala, Haskell and OCaml, so that's the common denominator.

However, although Rust wants to be an ML style language all the idioms break because there's no GC.


So I regularly jump between Scala and Rust and it's more a feeling.

In that overall the way option types, pattern matching, FP operations e.g. map/filter, immutability by default and type parameters have been implemented are very similar to Scala. And since these make up a large percentage of the everyday code you write you see the similarity as being larger than maybe it is.


All these are just functional features you find in every functional language though. Rust was highly influenced by OCAML and other ML languages


Hence why I said it was a feeling.

And I think the fact the overall style of the language e.g. braces, semicolon, method signatures, loop handling is so similar to Scala versus OCaml contributes to this.


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.


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


In F#, which brought the unit-of-measure feature to (relative) mainstream, there are no automatic translations between different units for the same measure, avoiding most of these traps. You can't add inches and feet - you have to explicitly convert one or the other first. Of course, there's still precision and rounding issues here, but no more so than with any other floating-point arithmetic expression you might write.




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

Search: