News for hackers. Stuff that matters.
Re:Air traffic failure caused by two locations 3600nm apart ... by astrange on Monday November 18, 2024 @07:23PM (#42178834)
Some languages do; F# and Ada have units.

I agree no sexy languages have it, and almost all languages have terrible support or anti-support for correctness in numerical programming. It's very strange.

(By anti-support I mean things that waste your time and make it harder. For instance, a lot of languages think "static typing" means they need to prevent you from doing `int a,b; short c = a * b;` even if this is totally well-defined.)

Re:Air traffic failure caused by two locations 3600nm apart ... by KerrAvon on Monday November 18, 2024 @07:43PM (#42178978)
`short c = a * b` can be both well-defined and a serious bug if (a * b) is greater than `sizeof(short)`. Whether it _is_ a bug depends on what you're doing.

Swift has units as part of the standard library. In the sense that matters here, Rust and C++ could also have units. It requires a level of expressiveness in the type system that most modern languages do have, if you put it to use.

Re:Air traffic failure caused by two locations 3600nm apart ... by astrange on Monday November 18, 2024 @07:47PM (#42179005)
It can be a serious bug if it overflows and you didn't intend it to happen or you expected overflow behavior to do something different. But that's also true of `int c = a*b`, and yet that's not a compiler error.

int/short should be thought of as storage size optimizations for memory. They're very bad ways to specify the correct range of values for a variable.

(Ada has explicitly ranged integers though!)

Re:Air traffic failure caused by two locations 3600nm apart ... by rkagerer on Tuesday November 19, 2024 @01:52PM (#42186852)
Whenever it's vague, I include the units as part of the parameter name (eg. delay_ms). Not perfect, but for practical purposes it helps. It's simple and I'm not sure why there aren't more people/libraries doing this.