Treating the abusive unsigned syndrome

bearophile bearophileHUGS at lycos.com
Wed Nov 26 16:09:01 PST 2008


Andrei Alexandrescu:
> That's also a possibility - consider unsigned types just "bags of bits" 
> and disallow most arithmetic for them. They could actually be eliminated 
> entirely from the core language because they can be implemented as a 
> library. I'm not sure how that would feel like.
> 
> I guess length would return an int in that case?

I don't know what the solution is, but I am very happy to see that in this newsgroup there are people willing to reconsider such basic things, to try to improve the language.
Most ideas turn out to be wrong, but if you aren't bold enough to consider them, there will no improvements :-)

In my programs I use use unsigned integers and unsigned longs as:
- bitfields, a single size_t, for example to represent a small set of items.
- bitarrays, in an array of size_t, to represent a larger set, to have array of bit flags, etc.
- to pack small variables into a uint, size_t, etc, for example use the first 5 bits to represent a, the following 2 bits to represent b, etc. In such situation I have never pack such variables into a signed int.
- when I need very large integer values, but this has to be done with care, because they can't be converted back to ints.
- I'd also like to use unsigned ints to denote that for example a function takes a nonnegative argument. I used to do this in Delphi, but I have seen it's too much unsafe in D, so now in D I prefer to use ints and then inside the function test for a negative argument and throw an exception (generally I don't use an assert for this but in the most speed critical situations).
- I use unsigned bytes in some situations, now and then. I don't use signed bytes anymore, I used to use them for 8 bit digital audio, but not anymore. Now 16 bit signed audio is the norm (a short) or even 24 bit (I have created a slow 24 bit value time ago).
- Probably there are few other situations, for example I think I've used an ushort once, but not many of them.

Bye,
bearophile



More information about the Digitalmars-d mailing list