Every story and comment from the Hacker News API, in a layout borrowed from 2001.
A comment on Names are not type safety (2020). Parent comment
a function `fn convertYardsToKm(value: i32): i32` doesn't fail when you give it a weight.
Whereas in Rust you'd write something like this:
#[derive(Copy, Clone)] struct Yard(i32); #[derive(Copy, Clone)] struct Km(i32); #[derive(Copy, Clone)] struct Lbs(i32); #[derive(Copy, Clone)] struct Kg(i32);
You can group them in an enum
enum Measurement { Yard(Yard(i32)), Km(Km(i32)), Lbs(Lbs(i32)), Kg(Kg(i32)), }
That way there is no confusion what you're putting in, and what type the output is, which has resulted in for example an emergency landing https://en.wikipedia.org/wiki/Gimli_Glider#Miscalculation_du... and loss of a Mars probe: https://en.wikipedia.org/wiki/Mars_Climate_Orbiter
There are no comments on this comment yet.
a function `fn convertYardsToKm(value: i32): i32` doesn't fail when you give it a weight.
Whereas in Rust you'd write something like this:
and your functions becomes `fn convertYardsToKm(value: Yard): Km`You can group them in an enum
(Note that it would be nice if we could refer to `Measurement::Yard` as a type vs have to add a distinct `Yard` type).That way there is no confusion what you're putting in, and what type the output is, which has resulted in for example an emergency landing https://en.wikipedia.org/wiki/Gimli_Glider#Miscalculation_du... and loss of a Mars probe: https://en.wikipedia.org/wiki/Mars_Climate_Orbiter