3 ms·
Sigh. “You don’t need unsigned types, just these annoying workarounds. Why use unsigned types when you can cast and bit fiddle!” The proof is in the use. If
by binarymax 4y ago
Sigh. “You don’t need unsigned types, just these annoying workarounds. Why use unsigned types when you can cast and bit fiddle!”
The proof is in the use. If people create and use libraries that abstract for a primitive that every other compiled language has, then the primitive is missing from your language.
- mqus 4y agowell, if only a minority uses those workarounds/libraries then maybe it's best to have it as a library and not in the language. That way the language can worry about other things instead of having to support twice as many integer types. I also sometimes want to have unsigned integer types, mostly for data model correctness sake (aka this value can't be negative anyways, lets use unsigned). But so far I only really missed unsigned bytes in java (which the author acknowledges). And even in other languages I rarely "need" unsigned types.
- pkolaczk 4y ago> if only a minority uses those workarounds/libraries Only a minority uses those workarounds, because they are cumbersome workarounds. If they were given proper unsigned types, they would use them much more often. I find myself using unsigned types in 99% of cases in Rust. I don't even remember the last time I had to use signed type.
- firstlink 4y agoThe only time I've ever used a signed integer rust is to store (temporarily) an offset of some unsigned value!
- mycall 4y agoHow do you almost never run in to negative numbers?
- wewtyflakes 4y agoDescribing the sizes of collections seems like a reasonable prolific example.
- Jensson 4y agoBut if you do any sort of math on those sizes then you probably want them to be signed anyway to avoid -1 rolling over to UINT_MAX and bugging out your system. Automatic signed to unsigned type coercion is extremely bug prone, so better avoid using unsigned integers entirely unless you never want to use them in math formulas. You avoid many more bugs by limiting collections to 2 billion than by mixing signed and unsigned integers. I have never had a bug related to signed collection sizes, but I have had a lot of bugs related to unsigned to signed casts.
- pkolaczk 4y agoThere is no automatic signed to unsigned coercion in Rust, so maybe that's why I don't have that problem. And I almost entirely use unsigned, even when doing math on sizes, so I never mix them. The only place one has to be careful is subtraction.
- jltsiren 4y agoWhen I write algorithmic code based on collections, the math is usually done on offsets, not sizes. And since negative offsets don't exist, situations where they could arise require explicit handling anyway. As I work in bioinformatics, the in-memory collections tend to be pretty large. While the arithmetic is usually done with 64-bit integers, it often makes sense to store the numbers in 32 bits to save space. And since the length of a human genome is ~3 Gbp, that means unsigned 32-bit integers. Signed 32-bit integers are just bugs waiting to happen. And sometimes the integers are stored in bit-packed arrays, where the width could be 29 bits, 33 bits, or something like that. Those are much easier and less error-prone with unsigned integers.
- wewtyflakes 4y ago> Automatic signed to unsigned type coercion is extremely bug prone When adding unsigned int as a language feature, don't we get to make up the rules? It seems like we can choose to make the rules not awful; we are not beholden to what C++ has done.