bearophile can say "i told you so" (re uint->int implicit conv)

Don turnyourkidsintocash at nospam.com
Thu Apr 4 00:54:17 PDT 2013


On Wednesday, 3 April 2013 at 14:54:03 UTC, Steven Schveighoffer 
wrote:
> On Wed, 03 Apr 2013 07:33:05 -0400, Don 
> <turnyourkidsintocash at nospam.com> wrote:
>
>> Yeah, but I think that what this is, is demonstrating what a 
>> useful concept a positive integer type is. There's huge value 
>> in statically knowing that the sign bit is never negative. 
>> Unfortunately, using uint for this purpose gives the wrong 
>> semantics, and introduces these signed/unsigned issues, which 
>> are basically silly.
>>
>> Personally I suspect there aren't many uses for unsigned types 
>> of sizes other than the full machine word. In all the other 
>> sizes, a positive integer would be more useful.
>
> Hm.. would it be useful to have a "guaranteed non-negative" 
> integer type?  Like array length.  Then the compiler could make 
> that assumption, and do something like what I did as an 
> optimization?
>
> Subtracting from that type would result in a plain-old int.
>
> -Steve

I think it would be extremely useful. I think "always positive" 
is a fundamental mathematical property that isn't captured by the 
type system. But I fear the heritage from C just has too much 
momentum.

One thing we could do immediately, without changing anything in 
the language definition at all, is add range propagation for 
array length.

So that, for any array A, A.length is in the range 0 .. 
(size_t.max/A[0].sizeof)
which would mean that unless A is of type byte, ubyte, void, or 
char, the length is known to be a positive integer. And of course 
for a static array, the exact length is known.

Although that has very limited applicability (only works within a 
single expression), I think it might help quite a lot.



More information about the Digitalmars-d mailing list