Re:Untyped Programs Don’t Exist
by int_19h on Thursday December 06, 2018 @10:38PM
(#18624953)
> I would suggest avoiding this kind of thinking. Set theory and type theory are separate.
There are some languages - notably, Ada - where this (types as sets) is very much explicit in the language.
> Why not just use u32 then?
For u32, 0 is "just another value" - there's nothing special about it. If you pass it to some other code, there's nothing telling them that they must check for zero, and must not treat zero as actual zero value (and if they forget, it will silently give the wrong answer).
Option<NonZeroU32> requires them to pattern-match on None/Some, and if they get Some, it's guaranteed to be non-zero. That None is stored as zero is an implementation detail in that case, and doesn't leak onto the abstraction level on which code operates.
There are some languages - notably, Ada - where this (types as sets) is very much explicit in the language.
> Why not just use u32 then?
For u32, 0 is "just another value" - there's nothing special about it. If you pass it to some other code, there's nothing telling them that they must check for zero, and must not treat zero as actual zero value (and if they forget, it will silently give the wrong answer).
Option<NonZeroU32> requires them to pattern-match on None/Some, and if they get Some, it's guaranteed to be non-zero. That None is stored as zero is an implementation detail in that case, and doesn't leak onto the abstraction level on which code operates.