"Better" / richer / more refined (aiming/aspiring at least!) type systems than C/++/Java/C#/Go/etc aim to make it ever-more "powerfully convenient, effortless, and free-of-cost to denote eg. such different units as uniquely distinct (yet 'compatible' when explicit conversion is finally expressly (and visibly (and searchably)) called for) types" that all source code cannot possibly mismatch accidentally without the compiler catching it --- however the issue remains that we don't, and possibly can't have type systems that also enforce such styles (rather than just relying on a developer's/team's own discipline & resolve to adhere to such convention even in the face of deadlines & budget/schedule pressures) unless we actually totally forbid primitives like lone (semantics ambiguous) ints, lone (likewise) floats etc. Similar to "bool blindness", there's the general issue of "primitive/scalar-type blindness" always lingering. Probably some PhD candidate will write a Haskell extension for such a scenario some day soon.
Though of course the next thing the deadline-driven developer will do is construct a single "semantic" int type used for all different semantic sorts kinds and types of "ints"......
https://blogs.msdn.microsoft.com/andrewkennedy/
https://channel9.msdn.com/Blogs/Charles/Andrew-Kennedy-F-Uni...
That this is a mistake that is possible to make in any language, but nevertheless this is also a problem that could be caught by a sufficiently good type system, if it was put to sufficiently good use. In fact, part of the justification for allowing user defined literals in C++ was precisely to make it easy and convenient to avoid mistakes like the Mars Climate Orbiter. Bjarne Stroustrup himself used that as an example in at least one talk he gave about C++11.
int temperature_a;
int temperature_b;
or even: Temperature a;
Temperature b;
you would have: Celsius a;
Fahrenheit b;
And an assignment between a and b would be an error, as Celcius and Fahrenheit are different types. Going along this path then, if you can extend the type system enough: Yards width;
Yards length;
Acre size = width * length; /* this would be okay */
Yards width;
Yards length;
Yards height;
Acre money_bin = width * length * height; /* error */
/* as acre is a measure of area, not volume */
Think of the type of calculations that the Unix command 'units' does, but built into a language instead of an application.You could wrap your value in a typed data structure:
enum LengthUnit {
Feet(f64),
Meters(f64),
}
And provide conversion functions between them, then only operate on one type, `LengthUnit::Meters` and throw errors if `LengthUnit::Feet` is passed in.I'm using Rust syntax here because it's fresher on my mind, but you could do the same with Haskell, OCaml, F#, etc. IIRC, OCaml would optimize away the outer structure so you wouldn't have much/if any performance hit. Presumably compilers for the other languages could/would do the same.
EDIT: for formatting and clarity.
There are legitimate gripes about C/C++, especially in a space with hostile actors an unknown inputs, but that example was particularly weak.