RFC on range design for D2

Lars Ivar Igesund larsivar at igesund.net
Tue Sep 9 09:36:38 PDT 2008


Andrei Alexandrescu wrote:

> 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)[]?

No, I actually referred to what you say below. My point was that ~= is an
unsafe operation on slices (not impossible as I apparently said), and thus
you need copy-on-write to be safe from erratic behaviour.

> 
> 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

-- 
Lars Ivar Igesund
blog at http://larsivi.net
DSource, #d.tango & #D: larsivi
Dancing the Tango


More information about the Digitalmars-d-announce mailing list