|
|
|
|
|
by Spivak
1678 days ago
|
|
This ends up being a super unergonomic API in practice because you would need to modify the type-checker to infer positive integers correctly. Nobody wants to go through the effort of NonZero = NewType(‘NonZero’, int)
if x != 0
x = NonZero(x)
just to call your function, especially since you still have to handle the case where someone does NonZero(x) blindly without checking. Should the constructor throw? |
|
The NonZero type should be responsible for checking the wrapped value is non-zero, you will probably want safe and unsafe constructor functions
where the unsafe version throws.If this is overkill for your application then I'd prefer throwing an exception in div rather than encoding the failure in the return type.