How to create a mutable array of strings?

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 06:55:51 PDT 2015


On Monday, 18 May 2015 at 13:14:38 UTC, Steven Schveighoffer 
wrote:
> It's annoying to have to dup each one.

Yes, it's really annoying. However, the problem can be solved as 
follows:
http://forum.dlang.org/thread/owxweucyzjwugpjwhwdu@forum.dlang.org?page=2#post-cqjevoldkqdkmdbenkul:40forum.dlang.org

On Monday, 18 May 2015 at 13:14:38 UTC, Steven Schveighoffer 
wrote:
> 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

Now imagine that you have a multi-dimensional array of strings. 
This will not work:

auto s = map!(a => a.dup)([["foo", "baz"], ["bar", 
"test"]]).array;

You have to apply to each line .dup :)

auto s = [["foo".dup, "baz".dup], ["bar".dup, "test".dup]];
s[1][0][1] = 't';

On Monday, 18 May 2015 at 13:14:38 UTC, Steven Schveighoffer 
wrote:
> 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.

Thank you. I do not know.
And yet, the problem is easily solved. You just have to add 
.deepDup Phobos:
http://forum.dlang.org/thread/owxweucyzjwugpjwhwdu@forum.dlang.org?page=2#post-cqjevoldkqdkmdbenkul:40forum.dlang.org


More information about the Digitalmars-d-learn mailing list