www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char[][] to std::vector<std::string> - DIP or dmd-issue?

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Hello, everyone!

I like to work with arrays of strings like `string[] strArray`, 
but unfortunately, they are immutable.

I do not like to work with arrays of strings such as `char[][] 
strArray`, because it is necessary to apply the method .dup each 
substring to make them work :)

I understand that the type of `string[]` to D is a simple data 
type than `char[][]`, but it seems to me that the problem is 
solved in C++:
std::vector<std::string> stdArray;

I wish to propose the creation of new types of data D: str, wstr, 
dstr, which will be the analogs of C++ `std::vector<std::string>`.

I do not know whether it is possible to create in the D, but I 
want to know where I write a sentence?
Can I file a dmd-issue, or should I create a DIP, because it is 
too big improvement?
Jun 13 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 15:21:19 UTC, Dennis Ritchie wrote:
 I wish to propose the creation of new types of data D: str, 
 wstr, dstr, which will be the analogs of C++ 
 `std::vector<std::string>`.
Ie str, wstr, dstr be mutable counterparts immutable strings respectively str (mutable(char[])), wstr (mutable(wchar[])), dstr (mutable(dchar[])). In C++: std::vector<std::string> str[], wstr[], dstr[] in C++: std::vector<std::vector<std::string> >
Jun 13 2015
prev sibling parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 13 June 2015 at 15:21:19 UTC, Dennis Ritchie wrote:
 Hello, everyone!

 I like to work with arrays of strings like `string[] strArray`, 
 but unfortunately, they are immutable.

 I do not like to work with arrays of strings such as `char[][] 
 strArray`, because it is necessary to apply the method .dup 
 each substring to make them work :)
Huh? You mean with string literals? That would be a rather silly reason to avoid `char[]`. Please show an example of .dup you'd like to avoid.
 I understand that the type of `string[]` to D is a simple data 
 type than `char[][]`,
Are you saying that `string[]` is simpler than `char[][]`? That's not true: `string` is an alias for `immutable(char)[]`, so `string[]` is the same as `immutable(char)[][]`.
 but it seems to me that the problem is solved in C++:
 std::vector<std::string> stdArray;

 I wish to propose the creation of new types of data D: str, 
 wstr, dstr, which will be the analogs of C++ 
 `std::vector<std::string>`.
Before jumping to a solution, please elaborate on the perceived problem. I have a feeling that there is none.
Jun 13 2015
next sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 15:45:34 UTC, anonymous wrote:
 Before jumping to a solution, please elaborate on the perceived 
 problem. I have a feeling that there is none.
Do you like to write? char[][] strArray = ["foo".dup, "bar".dup, "baz".dup]; I suggest that such an option: str[] strArray = ["foo", "bar", "baz"]; On Saturday, 13 June 2015 at 15:38:31 UTC, Dennis Ritchie wrote:
 Ie str, wstr, dstr be mutable counterparts immutable strings 
 respectively str (mutable(char[])), wstr (mutable(wchar[])), 
 dstr (mutable(dchar[])). In C++:
 std::vector<std::string>
std::string in C++.
 str[], wstr[], dstr[] in C++:
 std::vector<std::vector<std::string> >
std::vector<string> in C++.
Jun 13 2015
parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 13 June 2015 at 15:58:44 UTC, Dennis Ritchie wrote:
 On Saturday, 13 June 2015 at 15:45:34 UTC, anonymous wrote:
 Before jumping to a solution, please elaborate on the 
 perceived problem. I have a feeling that there is none.
Do you like to write? char[][] strArray = ["foo".dup, "bar".dup, "baz".dup];
Ok. That's all you're on about? Basically you'd like this: char[] s = "foo"; and this: char[][] a = [["foo"]]; etc. Yeah, that would be neat. But typing out ".dup" isn't that bad, and converting a `string[]` to a `char[][]` is simple: import std.conv: to; auto a = ["foo"].to!(char[][]);
 I suggest that such an option:
 str[] strArray = ["foo", "bar", "baz"];
I don't see how adding a new builtin type `str` would solve anything.
Jun 13 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 16:20:46 UTC, anonymous wrote:
 Do you like to write?
 char[][] strArray = ["foo".dup, "bar".dup, "baz".dup];
Ok. That's all you're on about? Basically you'd like this: char[] s = "foo"; and this: char[][] a = [["foo"]]; etc.
Yes. That's right, and not otherwise :)
 Yeah, that would be neat. But typing out ".dup" isn't that bad, 
 and converting a `string[]` to a `char[][]` is simple:
     import std.conv: to;
 auto a = ["foo"].to!(char[][]);
Yes, but it is not suitable for multidimensional array of strings.
 I suggest that such an option:
 str[] strArray = ["foo", "bar", "baz"];
I don't see how adding a new builtin type `str` would solve anything.
And why in C++ is running `std::vector<std::string>` ? Really in D can not do something like that? Maybe a new type will not solve anything, but there should be other ways to do in D analogue strings of C++. On Saturday, 13 June 2015 at 16:22:44 UTC, anonymous wrote:
 I don't understand what you're trying to say with that quote.
I would not say `simpler`, and `basic`. I just forgot the right word, because my English is not good enough.
Jun 13 2015
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Saturday, 13 June 2015 at 17:02:06 UTC, Dennis Ritchie wrote:
 On Saturday, 13 June 2015 at 16:20:46 UTC, anonymous wrote:
[...]
 Yeah, that would be neat. But typing out ".dup" isn't that 
 bad, and converting a `string[]` to a `char[][]` is simple:
     import std.conv: to;
 auto a = ["foo"].to!(char[][]);
Yes, but it is not suitable for multidimensional array of strings.
Please show how it is not. Seems to work just fine. [...]
 And why in C++ is running `std::vector<std::string>` ?
 Really in D can not do something like that?

 Maybe a new type will not solve anything, but there should be 
 other ways to do in D analogue strings of C++.
Your definitions of "something like that" and "other ways" are unreasonably narrow, in my opinion. Typing out ".dup" is D's way to do mutable strings. You just don't like it.
Jun 13 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 17:37:31 UTC, anonymous wrote:
 Please show how it is not. Seems to work just fine.
OK. Still, this method works: char[][][][][][] strArr = [[[[["foo", "baz"], ["bar", "tor"]]]].to!(char[][][][][])]; But I don't want to write this `.to!(char[][][][][])`. On Saturday, 13 June 2015 at 17:37:31 UTC, anonymous wrote:
 Your definitions of "something like that" and "other ways" are 
 unreasonably narrow, in my opinion. Typing out ".dup" is D's 
 way to do mutable strings. You just don't like it.
Yes, I don't like it.
Jun 13 2015
prev sibling parent reply "Kagamin" <spam here.lot> writes:
Type is probably possible, though conversion method will be 
simpler. You can even try to write a specialization of `to` for 
multidimentional arrays if it doesn't work.
Jun 13 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 17:39:25 UTC, Kagamin wrote:
 Type is probably possible, though conversion method will be 
 simpler. You can even try to write a specialization of `to` for 
 multidimentional arrays if it doesn't work.
It appears the problem can be solved by creating specifications .to!strArray, which will determine the dimension of the array and convert it to char[][][][]... Actually, I will file issue `std.conv` in Phobos to add such specifications. It will suit me. Thanks to all. I just didn't know that such a conversion is running.
Jun 13 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 18:15:30 UTC, Dennis Ritchie wrote:
 Actually, I will file issue `std.conv` in Phobos to add such 
 specifications. It will suit me.
*specializations
Jun 13 2015
prev sibling next sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 15:45:34 UTC, anonymous wrote:
 Huh? You mean with string literals? That would be a rather 
 silly reason to avoid `char[]`. Please show an example of .dup 
 you'd like to avoid.
Yes, string literals.
 I understand that the type of `string[]` to D is a simple data 
 type than `char[][]`,
Are you saying that `string[]` is simpler than `char[][]`? That's not true: `string` is an alias for `immutable(char)[]`, so `string[]` is the same as `immutable(char)[][]`.
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.
http://forum.dlang.org/post/mjctql$j19$1 digitalmars.com
Jun 13 2015
parent "anonymous" <anonymous example.com> writes:
On Saturday, 13 June 2015 at 16:09:58 UTC, Dennis Ritchie wrote:
 On Saturday, 13 June 2015 at 15:45:34 UTC, anonymous wrote:
[...]
 Are you saying that `string[]` is simpler than `char[][]`? 
 That's not true: `string` is an alias for `immutable(char)[]`, 
 so `string[]` is the same as `immutable(char)[][]`.
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.
http://forum.dlang.org/post/mjctql$j19$1 digitalmars.com
I don't understand what you're trying to say with that quote.
Jun 13 2015
prev sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Saturday, 13 June 2015 at 15:45:34 UTC, anonymous wrote:
 Please show an example of .dup you'd like to avoid.
For example, if you need to create a five-dimensional array of strings :)
Jun 13 2015