8 ms·
One stylistic nitpick / question. It says: > "We expected an integer, but we got (). () is pronounced 'unit', and is a special type in Rust's type system. () i
by SideburnsOfDoom 12y ago
One stylistic nitpick / question. It says:
> "We expected an integer, but we got (). () is pronounced 'unit', and is a special type in Rust's type system. () is different than null in other languages, because () is distinct from other types"
Would it not be more accurate and more informative to compare the "special type" unit to "void" than to compare it to "null" ?
The keyword "void" is a placeholder that says "nothing here" where normally there would be a type. It function more or less like a type. i.e. "public void foo { ... };" instead of "public int foo() { ... };"
I understand "unit" as a "first class void".
"null" on the other hand is a value, that can be placed in variables of many types.
- the_mitsuhiko 12y agoThe problem with void is that it's not a real type in C.
- SideburnsOfDoom 12y agoYes, totally. The same in java and c# too. That's why I said void is "more or less like a type" and asked about "unit" as a "first class void" - i.e. a "void" that is a real type that works like other types.
- mercurial 12y agoWell, in Java you have http://docs.oracle.com/javase/7/docs/api/java/lang/Void.html http://docs.oracle.com/javase/7/docs/api/java/lang/Void.html Not exactly first-class but useful at times.
- jeremyjh 12y agoNull is not a type either, and you cannot reference it in a definition. I think void is a closer match.
- AaronFriel 12y agoAs someone else describes, void is the "uninhabited" type, i.e.: it cannot be created. Void is a way of indicating the absence of valid output. I think null is wrong too, because it references the fact that in most languages, types are a sum type of "all possible valid inputs" plus "null". Rust avoids null and has no null pointers unless you use unsafe code. Unit means "there is precisely one instance of this type". It is the unitary type.
- stonemetal 12y agoFirst you have to say if you are talking about () the type or () the value. () the type is fairly close to void in meaning. () the value is fairly close to null in meaning. Thus both comparisons are accurate, which is applicable depends on which () you are referring to. While your post is correct for () the type, if I consider () the value, your post could be transformed without loss of accuracy to: Void is not a value either, and you cannot return it from a function. I think null is a closer match. Value or type neither half is the whole story.
- bjz_ 12y agoI guess you could argue that 'null' refers to the '1' in a sum type 'T + 1'. I still think 'unit' is the most precise term though, especially seeing as Rust doesn't have structural sum types. `None` is the closest we get to null.
- deleted 12y ago[deleted]
- lelf 12y agoIt's called unit in math / many other languages. null is not a type. void type is inhabited. unit type has exactly one value, ().
- sratner 12y agoComparison with `null` is strange. First thing I thought of was JavaScript's `undefined`.