Jump to page: 1 2 3
Thread overview
How to create a mutable array of strings?
May 17, 2015
Dennis Ritchie
May 17, 2015
Dennis Ritchie
May 17, 2015
Dennis Ritchie
May 18, 2015
Dennis Ritchie
May 18, 2015
Dennis Ritchie
May 17, 2015
Jack Applegame
May 17, 2015
Daniel Kozak
May 17, 2015
Daniel Kozak
May 17, 2015
Dennis Ritchie
May 17, 2015
anonymous
May 17, 2015
Dennis Ritchie
May 17, 2015
anonymous
May 17, 2015
Dennis Ritchie
May 17, 2015
Daniel Kozak
May 17, 2015
Dennis Ritchie
May 17, 2015
Marc Schütz
May 17, 2015
Daniel Kozak
May 17, 2015
Dennis Ritchie
May 17, 2015
Daniel Kozak
May 17, 2015
Daniel Kozak
May 17, 2015
Dennis Ritchie
May 17, 2015
Daniel Kozak
May 17, 2015
Hi,
It seems to me, or D do not create mutable array of strings?

How to create a mutable equivalent of a string array?

-----
string[] s = ["foo", "bar"];
// s[1][1] = 't'; // immutable expression s[1][1]
May 17, 2015
On Sunday, 17 May 2015 at 09:06:40 UTC, Dennis Ritchie wrote:
> Hi,
> It seems to me, or D do not create mutable array of strings?
>
> How to create a mutable equivalent of a string array?
>
> -----
> string[] s = ["foo", "bar"];
> // s[1][1] = 't'; // immutable expression s[1][1]

It's uncomfortable:
-----
char[][] s = [['f', 'o', 'o'], ['b', 'a', 'r']];
s[1][1] = 't';
May 17, 2015
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.
May 17, 2015
On Sun, 17 May 2015 09:06:38 +0000
Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> Hi,
> It seems to me, or D do not create mutable array of strings?
> 
> How to create a mutable equivalent of a string array?
> 
> -----
> string[] s = ["foo", "bar"];
> // s[1][1] = 't'; // immutable expression s[1][1]

auto s = ["foo".dup, "bar".dup];
May 17, 2015
On Sunday, 17 May 2015 at 09:10:06 UTC, Dennis Ritchie wrote:
> It's uncomfortable:
> -----
> char[][] s = [['f', 'o', 'o'], ['b', 'a', 'r']];
> s[1][1] = 't';
auto s = ["foo".dup, "bar".dup];
s[1][1] = 't';


May 17, 2015
On Sun, 17 May 2015 09:06:38 +0000
Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
wrote:

> Hi,
> It seems to me, or D do not create mutable array of strings?
> 
> How to create a mutable equivalent of a string array?
> 
> -----
> string[] s = ["foo", "bar"];
> // s[1][1] = 't'; // immutable expression s[1][1]

or you can use cast if you are sure thats what you really need:
auto s = [cast(char[])"foo", cast(char[])"bar"];

or

auto s = cast(char[][])["foo", "bar"];
May 17, 2015
On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
> auto s = cast(char[][])["foo", "bar"];

Thanks. This version I was completely satisfied.
May 17, 2015
On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>
> On Sun, 17 May 2015 09:06:38 +0000
> Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
>
>> Hi,
>> It seems to me, or D do not create mutable array of strings?
>> 
>> How to create a mutable equivalent of a string array?
>> 
>> -----
>> string[] s = ["foo", "bar"];
>> // s[1][1] = 't'; // immutable expression s[1][1]
>
> or you can use cast if you are sure thats what you really need:
> auto s = [cast(char[])"foo", cast(char[])"bar"];
>
> or
>
> auto s = cast(char[][])["foo", "bar"];

That's not a good idea. I haven't checked, but this will likely segfault on mutation, because the string literals are placed into read-only memory. And multiple identical strings are merged (shared), so that this would modify all occurances of that string literal, even if it shouldn't segfault.
May 17, 2015
On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>
> On Sun, 17 May 2015 09:06:38 +0000
> Dennis Ritchie via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> wrote:
>
>> Hi,
>> It seems to me, or D do not create mutable array of strings?
>> 
>> How to create a mutable equivalent of a string array?
>> 
>> -----
>> string[] s = ["foo", "bar"];
>> // s[1][1] = 't'; // immutable expression s[1][1]
>
> or you can use cast if you are sure thats what you really need:
> auto s = [cast(char[])"foo", cast(char[])"bar"];
>
> or
>
> auto s = cast(char[][])["foo", "bar"];

But I am unsure if this will work in case where immutable data will occupied read only memory.
May 17, 2015
On Sunday, 17 May 2015 at 09:20:17 UTC, Dennis Ritchie wrote:
> On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote:
>> auto s = cast(char[][])["foo", "bar"];
>
> Thanks. This version I was completely satisfied.

Remember that Daniel Kozak wrote "if you are sure thats what you really need". I'm confident that you're not sure it's what you need. For starters, this crashes on linux:

----
auto s = cast(char[][])["foo", "bar"];
s[1][1] = 't';
----
« First   ‹ Prev
1 2 3