3 ms·
> as name of the type, I think I like void more than () It's the wrong name though. In type theory, (), the type with one member, is traditionally called "Unit
by ruuda 1y ago
> as name of the type, I think I like void more than ()
It's the wrong name though. In type theory, (), the type with one member, is traditionally called "Unit", while "Void" is the uninhabited type. Void is the return type of e.g. abort.
- ivanjermakov 1y agoIf I understood `abort` semantics correctly, it has a type of `never` or Rust's `!`. Which has a meaning "unobtainable value because control flow went somwhere else". `void` is closer to `unit` or `()` because it's the type with no allowed values. Cool trick: some languages (e.g. TypeScript) allow `void` generics making parameters of that type optional.
- steveklabnik 1y agoUnit has exactly one allowed value.
- ivanjermakov 1y agoRight, but there is not much semantic difference in having one or having zero allowed values.
- NobodyNada 1y agoThere's a huge semantic difference: a type with zero allowed values can never be constructed. This means that a function that returns an uninhabited type is statically known to not return -- since there's no way it could construct an uninhabited value for a return expression. It also means you can coerce a value of uninhabited type into any other type, because any code which recieves a value of an uninhabited type must be unreachable. For instance, in Rust you can write the following: let weekday_name: &str = match weekday_number { 1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday", i => panic!("invalid weekday number: {i}); }; Because panic! aborts the program and doesn't return, its return type is uninhabited. Thus, a panic! can be used in a context where a string is expected.
- ivanjermakov 1y agoWe were talking about `void`, not `!`. It's clear that "never" type has a special language support. Difference between `void` and `()` is much less subtle.
- steveklabnik 1y agoOne vs zero is an incredibly fundamental difference.
- bmacho 1y ago[deleted]
- steveklabnik 1y agoIt doesn’t work because you’re trying to pass unit to sayhi, which doesn’t take any arguments.
- deleted 1y ago[deleted]
- cornstalks 1y agoIt fulfills the same role as C and C++'s void type. I don't think most systems programmers care about type theorist bikeshedding about purity with formal theory. A ton of people coming to Zig are going to be coming from C and C++. void is fine.
- netbsdusers 1y agoThe void type has considerable heritage, dating back all the way to ALGOL 68, and is traditionally defined as having one member: > The mode VOID has a single value denoted by EMPTY.