Arrays are sufficient for ArrayLists? Really??

Timon Gehr timon.gehr at gmx.ch
Mon May 16 13:14:33 PDT 2011


Steven Schveighoffer wrote:
> On Mon, 16 May 2011 15:52:23 -0400, Mehrdad <wfunction at hotmail.com> wrote:
>
>> On 5/16/2011 12:53 PM, Timon Gehr wrote:
>>>> In fact I even need to take that back. In order to work correctly, the
>>>> function would have to iterate downwards. It _is_ indeed buggy, and I
>>>> should stop emitting opinions when I'm short on time...
>>>>
>>>> Andrei
>>>
>>> Whoops, you are right:
>>>
>>> void removeAt(T)(ref T[] arr, size_t index)
>>> {
>>>     foreach (i, ref item; retro(arr[1 .. index+1]))
>>>          item = arr[i - 1];
>>>      arr = arr[1 .. $];
>>> }
>>>
>>> Timon
>>
>> Wouldn't that stomp on the super-slice of arr, though?
>>
>> As was pointed out on SO, the problem is actually always there: if
>> someone passes arr[0 .. $] to a function, it will look as if the
>> original array was passed, although it wasn't. Seems like it's a lot
>> uglier than I'd thought...
>
> arr[0..$] shouldn't be an lvalue (it is, but I think that's a bug).
> Therefore, you shouldn't be able to pass it as a ref argument (is this in
> bugzilla?).
>
> -Steve

I think it is a little bit more subtle than "not lvalue", because you actually
have to be able to do something like:

arr[0..2] = arr[3..5];

Where a slice appears on the left hand side of the assignment. (also a[]=b[]).


Timon


More information about the Digitalmars-d mailing list