Treating the abusive unsigned syndrome

Nick Sabalausky a at a.a
Tue Nov 25 15:05:18 PST 2008


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:gghsa1$2u0c$1 at digitalmars.com...
> Andrei Alexandrescu:
>> The problem is, it's much more annoying than one might imagine. Even
>> array.length - 1 is up for scrutiny. Technically, even array.length + 1
>> is a problem because 1 is really a signed int. We could provide
>> exceptions for constants, but exceptions are generally not solving the
>> core issue.
>
> That can be solved making array.length signed.
> Can you list few other annoying situations?
>

I disagree. If you start using that as a solution, then you may as well 
eliminate unsigned values entirely.

I think the root problem with disallowing mixed-sign operations is that math 
just doesn't work that way. What I mean by that is, disallowing mixed-sign 
operations implies that we have these nice cleanly separated worlds of 
"signed math" and "unsigned math". But depending on the operator, the 
signs/ordering of the operands, and what the operands actually represent, 
math has tendancy to switch back and forth between the signed ("can be 
negative") and unsigned ("can't be negative") worlds. So if we have a type 
system that forces us to jump through hoops every time that world-switch 
happens, and we then decide that it's justifiable to say "well, let's fix it 
for array.length by tossing that over to the 'can be negative' world, even 
though it cuts our range of allowable values in half", then there's nothing 
stopping us from solving the rest of the cases by throwing them over the 
"can be negative" wall as well. All of a sudden, we have no unsigned.

Just a thought: Maybe some sort of built-in "units" system could help here? 
Instead of just making array.length a "signed" or "unsigned" and leavng it 
as that, add a "units system" and tag array.length as being a length, with 
length tags carring the connotation that negative is disallowed. 
Adding/subtracting a pure constant to a length would cause the constant to 
be automaticlly tagged as a "length delta" (which can be negative). And the 
units system would, of course, contain the rule that a length delta 
added/subtracted from a length results in a length. The units system could 
then translate all of that into "signed vs unsigned". 





More information about the Digitalmars-d mailing list