RFC on range design for D2

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Sep 9 09:26:37 PDT 2008


Lars Ivar Igesund wrote:
> Andrei Alexandrescu wrote:
> 
>> A slice is a range alright without any extra adaptation. It has some
>> extra functions, e.g. ~=, that are not defined for ranges.
> 
> Aren't slices const/readonly/whatnot and thus ~= not possible without
> copying/allocation?

Well there's no change in semantics of slices (meaning T[]) between D1 
and D2, so slices mean business as usual. Maybe you are referring to 
strings, aka invariant(char)[]?

Anyhow, today's ~= behaves really really erratically. I'd get rid of it 
if I could. Take a look at this:

import std.stdio;

void main(string args[]) {
     auto a = new int[10];
     a[] = 10;
     auto b = a;
     writeln(b);
     a = a[1 .. 5];
     a ~= [ 34, 345, 4324 ];
     writeln(b);
}

The program will print all 10s two times. But if we change a[1 .. 5] 
with a[0 .. 5] the behavior will be very different! a will grow "over" 
b, thus stomping over its content.

This is really bad because the behavior of a simple operation ~= depends 
on the history of the slice on the left hand side, something often 
extremely difficult to track, and actually impossible if the slice was 
received as an argument to a function.

IMHO such a faux pas is inadmissible for a modern language.


Andrei


More information about the Digitalmars-d-announce mailing list