Hacker News new | ask | show | jobs
by andrewjf 7 days ago
Yeah but why wouldn't "x" be a type that could only represent positive integers?
4 comments

I would like Ada-style integer types too, but I don't strictly know how those are implemented. Are you suggesting invariants should also be given at the type level?
There is no builtin integer type that guarantees x > 0. I agree that adding such a type (and more general, range guarantees at the type system level) would be very useful.
Have you seen the wave of recent Whatever2Shell CVEs? Several of them happened precisely because "a type that could only represent positive integers" was used for the sizes.
I don't think that's what he's talking about. The problem with those CVEs is an unsigned type with default wrap semantics. But it need not be like that. Look at how Ada allows user-defined integers.
The user-defined integer types in Ada are either a subrange of the signed integers, or have modular semantics exactly as that that "unsigned T" have in C/C++. If we're talking about the additional range checks, they're only performed on an assignment to a variable. So, no, I don't exactly see how Ada's semantics would help here.
I think it's because of the overflow behavior of the types/operators used, not so much the fact that they only represent positive integers.
They really should just copy Ada rather than this whizbangery.
It would even be more cumbersome to add that and still allow it to interoperate with legacy code. The C and Rust types, or even the more powerful C++ types today would all still have to work in mixed code.

A shame, but that's life.