Treating the abusive unsigned syndrome
Sergey Gromov
snake.scaly at gmail.com
Wed Nov 26 15:18:03 PST 2008
Wed, 26 Nov 2008 15:57:55 -0600, Andrei Alexandrescu wrote:
> Sergey Gromov wrote:
>> Wed, 26 Nov 2008 09:12:12 -0600, Andrei Alexandrescu wrote:
>>
>>> Don wrote:
>>>> My suspicion is, that if you allowed all signed-unsigned operations when
>>>> at least one was a literal, and made everything else illegal, you'd fix
>>>> most of the problems. In particular, there'd be a big reduction in
>>>> people abusing 'uint' as a primitive range-limited int.
>>> Well, part of my attempt is to transform that abuse into legit use. In
>>> other words, I do want to allow people to consider uint a reasonable
>>> model of natural numbers. It can't be perfect, but I believe we can make
>>> it reasonable.
>>>
>>> Notice that the fact that one operand is a literal does not solve all of
>>> the problems I mentioned. There is for example no progress in typing u1
>>> - u2 appropriately.
>>>
>>>> Although it would be nice to have a type which was range-limited, 'uint'
>>>> doesn't do it. Instead, it guarantees the number is between 0 and
>>>> int.max*2+1 inclusive. Allowing mixed operations encourages programmers
>>>> to focus the benefit of 'the lower bound is zero!' while forgetting that
>>>> there is an enormous downside ('I'm saying that this could be larger
>>>> than int.max!')
>>> I'm not sure I understand this part. To me, the larger problem is
>>> underflow, e.g. when subtracting two small uints results in a large uint.
>>
>> I'm totally with Don here. In math, natural numbers are a subset if
>> integers. But uint is not a subset of int. If it were, most of the
>> problems would vanish. So it's probably feasible to ban uint from
>> SafeD, implement natural numbers by some other means, and leave uint for
>> low-level wizardry.
>
> 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 guess so. Actually, simply disallowing signed<=>unsigned cast and
making length signed would force most people to abandon unsigned types.
And moving unsgned types documentation in a separate chapter would warn
newcomers about their special status. Not a lot of changes on the
compiler side, mostly throwing stuff away.
More information about the Digitalmars-d
mailing list