How to create a mutable array of strings?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 06:14:39 PDT 2015


On 5/17/15 5:15 AM, Dennis Ritchie wrote:
> This option is also a strange:
>
> char[][] s = ["foo".dup, "bar".dup];
> s[1][1] = 't';
>
> In my opinion, you need to add to D keyword mutable.

It's annoying to have to dup each one.

But, you do have a couple other possibilities:

auto s = ["foo".dup, "bar".dup];

import std.algorithm : map;
import std.array : array;
auto s = map!(a => a.dup)(["foo", "bar"]).array; // this will needlessly 
allocate an array for the strings

But really, a string is immutable. There's not a way around that. A 
string is the most basic level of array primitive, not even mutable 
arrays of non-char types have that, and it's an annoyance. From there, 
you have to build the data out of ROM into the heap.

-Steve


More information about the Digitalmars-d-learn mailing list