[Submission] D Slices
Steven Schveighoffer
schveiguy at yahoo.com
Wed Jun 1 06:14:49 PDT 2011
On Tue, 31 May 2011 16:16:37 -0400, eles <eles at eles.com> wrote:
>> if n is unsigned int, and 0, then this becomes i = 0; i <=
> uint.max; i++
>> Basically, using subtraction in loop conditions is a big no-no.
>
> Yes, I have been trapped there. More than once. And yes, n=0 is a
> special case. An yes, is a big no-no. It also appears when *i* is
> unsigned int and you are decrementing it and comparing (with egality)
> against 0. The loop is infinite.
>
> For me, conceptually, the problem is simply that unsigned types, once
> at zero, should throw exception if they are decremented. It is
> illogical to allow such operation. However, type information is only
> available at the compile time, so when the program is running is
> difficult to take measures against it.
This is not practical. It would be too expensive to check because the
hardware does not support it.
> I thought, however, that those exception should be thrown in the
> "debug" version.
>
> However, there is no reason to make, instead, UTYPE_MAX a big no-no.
As has been said, multiple times, UTYPE_MAX is not a valid index, and that
is not because of the open-interval on the right. It's because of
addressing in a zero-based index world, you simply can't have an array
bigger than your address space. An array with UTYPE_MAX as a valid index
must have at least UTYPE_MAX + 1 elements. How this possibly can be a
"huge factor" for you, but having to put +1 and -1 all over the place for
mundane array indexing is beyond my comprehension. If you are looking for
an iron-clad reason for closed-right indexing, this isn't it.
But, it's not really important anyways. The open-right indexing ship has
not only sailed, it's made it across the ocean, formed several colonies,
and is currently declaring independence. You're about a decade too late
with this argument.
How much of a possibility would you think Matlab has of changing its
indexing scheme to be like D's? About the same chance as D adopting
Matlab's, I'd say. If this is a reason you will not use D, then I'm sorry
that you won't be with us, but that's just life.
-Steve
More information about the Digitalmars-d
mailing list