[Submission] D Slices

eles eles at eles.com
Tue May 31 13:28:33 PDT 2011


> a few reasons (in no particular order):
> 1. After years of using D, I've never had a problem with it working
that
> way. And even initially, I found it very easy to learn and get used
to it.

You become an "educated" user. How many? I bet that *you* will never
write

while(some_condition);
 do_some_intended_loop_processing();

but this happens!

What to do when it happens?

> 2. I've found it easier to avoid off-by-one errors. I don't have to
think
> about them as much.

Why? I fail to see the reason behind. It would be simply as
substracting 1 from the right limit. Is this more prone to errors?

> 3. arr[a..b].length == b-a     <-- That's a *very* nice, clean,
useful
> property to have. And I think it's one of the main reasons for #2
above. In
> fact, this actually makes it feel more balanced to me than having
inclusive
> on both ends.

Speaking of this, using an alternative syntax such as a
[slice_beginning..slice_length] would even allow you to not even make
that substraction in the first place, let alone adding 1.

But the fact is that when you are counting numbers going from 6 to 9,
to find *the number of numbers* you do: 9-6 PLUS 1.

> 4. The following:
> string str = "ABCDEF";
> int splitIndex = 3;
> string part1 = str[0 .. splitIndex];
> string part2 = str[splitIndex .. $];
> assert(part1 ~ part2 == str);
> Ie, when you split an array, you can use the same index for both
halves. No
> "+1"-ing. It just works. Don't have to think about it.

You can live a life without having to think about many things.
However, experience proves that thinking is generally better than not
"having to think about it". What if a bug?

Is not more logically to index those halves with:

a[0..length_half-1]
a[length_half..length-1]

We speak about *disjoint* halves here. Why not disjoint indexes?

What if you have a "center" (common) character that you want to keep
in both halves?

You would have to write:

string part1 = str[0 .. splitIndex+1];
string part2 = str[splitIndex .. $];

Is this more logical than writing:

string part1 = str[0 .. splitIndex];
string part2 = str[splitIndex .. $];

?





More information about the Digitalmars-d mailing list