3 ms·
Pascal worked that way all the time, and it was hated. You could have "inch" and "meter" version of integer, and they were not interchangeable. This was sometim
by Animats 3y ago
Pascal worked that way all the time, and it was hated. You could have "inch" and "meter" version of integer, and they were not interchangeable. This was sometimes called "strong typing"
It's interesting that in Rust, "type" does not work that way. I kind of expected that it would. But no, "type" in Rust is just an alternate name, like "typedef" in C.
- earleybird 3y agoNASA might have some thoughts on mixing inch and meter types :-) https://en.wikipedia.org/wiki/Mars_Climate_Orbiter https://en.wikipedia.org/wiki/Mars_Climate_Orbiter
- kevincox 3y agoBoth approaches are useful at different times. For example you wouldn't want to accidentally multiple a meter by a centimeter but you may want to provide std::io::Result<T> which is equivalent to Result<T, std::io::Error> but just a bit nicer to type. For example in Rust you can do: type Foo = Bar; Which is just an alias, interchangeable with Bar. Or you can do: struct Foo(Bar); Which is a completely new type that just so happens to contain a Bar.
- exceptione 3y agoIt is a form of strong typing because integer could be the length of your toe nail, a temperature or the seconds since the unix epoch. Sometimes you really want to make sure someone is not going to introduce billion dollar bugs, by making the type different from the underlying representation. In Haskell that would be sth like newtype Temperature = Int At other times, you just want to document in stead of forcing semantics. A contrived example: type AgeMin = Int type AgeMax = Int isAdmissible :: AgeMin -> AgeMax -> Bool isAdmissible :: Int -> Int -> Bool // less clear
- mc10 3y agoThe AgeMin/AgeMax example seems more of a deficiency due to a lack of named function parameters; it would be equally clear if it had a type signature (using OCaml as an example) of val is_admissible : min_age:int -> max_age:int -> bool
- exceptione 3y agoThere are other places were you use types as well. Also, if you later decide that an Age should not be int, but a string, you wont miss it in refactoring whereas in your example you don't have that facility.