Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 17, 2015 How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 Re: How to create a mutable array of strings? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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';
----
|
Copyright © 1999-2021 by the D Language Foundation