by Animats · 2y ago · 0 comments
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.
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.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 val is_admissible : min_age:int -> max_age:int -> boolAlso, 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.