Array slice length confusion
Tim Matthews
tim.matthews7 at gmail.com
Tue Jul 7 23:48:31 PDT 2009
First off if this has been asked so many times please just ignore this.
You should be able to collapse this whole subject in most good new
readers plus it is in d.learn out of the way so I expect no "ahh here we
go again" sort of replies.
Anyway I like how you can get a slice of array that is just a view into
the original. If I modify the slice, it modifies the full original array
which can be very useful. If however I modify the length of the slice
which I was doing in an attempt to extend the view, it becomes it's own
array.
This example should help explain what I mean:
module test;
import std.stdio;
void main()
{
char[5] a = "Hello";
char[] b;
b = a[1..3]; //b is a view into a
writeln(a); //(Hello)
writeln(b); //(el)
b[0] = 'a'; //no copy on write
writeln(a); //(Hallo)
writeln(b); //(al)
b.length = b.length+2; //COW on length change?
b[0] = 'z'; //does not modify a
writeln(a); //(Hallo)
writeln(b); //(zl)
}
Was this a design choice, bug, undefined behavior, etc...?
More information about the Digitalmars-d-learn
mailing list