Why I Use D

Janice Caron caron800 at googlemail.com
Mon Mar 31 01:02:23 PDT 2008


On 31/03/2008, Leandro Lucarella <llucax at gmail.com> wrote:
> I really can't see any overhead in array[5..-1] being syntax sugar for
>  array[5..$-1].

You can't? Well, I can. Let me show you.

    struct MyArray
    {
        MyArray opSlice(int lower, int upper)
        {
            ...
        }
    }

    MyArray array;
    auto slice = array[5..-1];

This calls MyArray.opSlice(5, -1). Are you suggesting it should
instead call MyArray.opSlice(5, __dollar-1)? Can you not just see the
bug reports flying in?

And here's a second problem

    int[] array;
    int n = -1;

    slice1 = array[5..-1];
    slice2 = array[5..n];

You'd expect slice1 and slice2 to be the same, but in fact array[5..n]
will throw in a debug build, and cause a segfault in a release build.
Not good.



More information about the Digitalmars-d mailing list