type of concatenated arrays
Steven Schveighoffer
schveiguy at yahoo.com
Wed Nov 7 13:00:20 PST 2007
In D 2, What should the return type be of concatenated const arrays?
For example, if I have a const(char)[] array a1, and I want to concatenate
"\n", I could do:
auto a2 = a1 ~ "\n";
Now, a2 is declared as a const(char)[] array, but I think it should be just
a char[] array. Why? because the concat operator should have made a copy
of the array with length +1 and added the \n character. If this is the
case, I should be able to do whatever I want with the copy. Why does it
have to be const?
If I want a mutable copy I am forced to do:
auto a2 = (a1 ~ "\n").dup;
which makes a copy of a temporary copy, or if I want to be more efficient, I
am forced to do something funky like:
char[] a2 = new char[a1.length + "\n".length];
a2[0..a1.length] = a1[];
a2[length - 1] = '\n';
But now, I think the new operation wastes time initializing the array before
copying the slice.
thoughts?
-Steve
More information about the Digitalmars-d
mailing list