digitalmars.D - About string and int spliced together.
- zoujiaqing (33/33) Jun 18 2020 Now dlang string processing is complex, like this:
- Dennis (7/8) Jun 18 2020 While this could be made to work for floating point numbers, it
- zoujiaqing (2/11) Jun 18 2020 Yes! You are right! Thanks!
- Nick Treleaven (4/10) Jun 19 2020 This is actually an open issue (someone filed it as a duplicate
- aberba (6/11) Jun 20 2020 So has D gotten so complicated under the hood that its not
- Adam D. Ruppe (28/30) Jun 20 2020 In that proposal, i"" is a string BUILDER, not a string. So it
- aberba (2/8) Jun 20 2020 I glad it works that way. It was clear what was happening there.
- zoujiaqing (6/15) Jul 30 2020 It's still too complicated.
- H. S. Teoh (9/16) Jul 30 2020 You could just use std.conv.text:
- Steven Schveighoffer (4/27) Jul 31 2020 what's complicated about:
- zoujiaqing (11/39) Jul 31 2020 Thanks Steve.
- Steven Schveighoffer (42/65) Aug 03 2020 So we have 2 alternatives here. Both are viable, and both have
- Stanislav Blinov (22/27) Jun 18 2020 No, it shouldn't. `string` is not a class, it's simply an array
- Kagamin (3/15) Jun 18 2020 You can use https://dlang.org/phobos/std_conv.html#text
- JN (6/9) Jun 18 2020 That looks actually quite nifty, I'll have to use that someday.
- Seb (3/12) Jun 18 2020 You can just add a "import std" at the top of your module and
- Kagamin (3/10) Jun 18 2020 `text` function is shorter, it's close to interpolated string in
- Steven Schveighoffer (4/11) Jun 18 2020 age.to!string will allocate a temporary string just to throw it away.
- Stanislav Blinov (4/7) Jun 18 2020 Wouldn't it?
- Steven Schveighoffer (7/17) Jun 18 2020 Yikes. Let me rephrase.
- Stanislav Blinov (4/16) Jun 18 2020 Let's rephrase it then ;)
- Nick Treleaven (11/16) Jun 19 2020 It used to call std.conv.toChars but that triggered a dmd
- Steven Schveighoffer (4/23) Jun 19 2020 I think we should revert back to toChars, and see what the test suite
- Nick Treleaven (5/11) Jun 19 2020 OK. I added the two test cases in the issue to
- Stanislav Blinov (3/12) Jun 19 2020 Am I reading this right? Library was downgraded because of a
- Steven Schveighoffer (11/26) Jun 19 2020 I don't think it's a regression in the compiler, I think it's a bug that...
- Kagamin (26/44) Jun 19 2020 I reduced it a but, but then it started to depend on the order of
- Kagamin (24/24) Jun 20 2020 Reduced std.uni
- mw (64/65) Jun 20 2020 We've talked too much on this topic, I've found a number of DIPs,
Now dlang string processing is complex, like this: ```D import std.stdio; import std.conv; void main() { string name = "Brian"; uint age = 18; string text = "My name is " ~ name ~ " and my age is " ~ age.to!string ~ "."; writeln(text); } ``` Type should be automatically converted to string! It should be: ```D import std.stdio; void main() { string name = "Brian"; uint age = 18; string text = "My name is " ~ name ~ " and my age is " ~ age ~ "."; writeln(text); } ``` Like java: ```D String name = "Brian"; int age = 18; System.out.println("My name is " + name + " and my age is " + age + "."); ```
Jun 18 2020
On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C' You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186
Jun 18 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:Yes! You are right! Thanks!Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C' You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186
Jun 18 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:This is actually an open issue (someone filed it as a duplicate of a more subtle bug though): https://issues.dlang.org/show_bug.cgi?id=18346Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C'
Jun 19 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:Type should be automatically converted to string!You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186So has D gotten so complicated under the hood that its not possible to d: string sentence = i"I and ${expression}"; Cut I'm seeing some weird syntax in there. Nothing like anything from JavaScript as referenced.
Jun 20 2020
On Saturday, 20 June 2020 at 11:09:12 UTC, aberba wrote:So has D gotten so complicated under the hood that its not possible to d:In that proposal, i"" is a string BUILDER, not a string. So it returns an object you can get a string out of, or do other things with too. implicitly converts to string on demand, inserting a hidden call to its toString method when you assign it like that, and the Javascript one is passed to a function that does the conversion with special built-in syntax. JS' foo`$bar` actually calls the function foo to convert the string builder object to whatever you want, and if you don't specify a function, the language inserts a hidden toString function for you automatically. The DIP there works the same way as those, just without the hidden function calls. You need to choose what function you want to call yourself. // so this works since you call the idup method yourself // telling it to copy the result of the builder into a // plain string string sentence = i"I and ${expression}".idup; Or you can call other functions and avoid copying for maximum efficiency and flexibility - no hidden functions to surprise the low-level D crowd while only needing a very simple function call if you don't care about that and just want to keep it simple. Similar to how D's map, filter, etc. may need the extra call to .array in some use cases. It looks like extra work at first glace, but it actually enables better code once you get to know it.
Jun 20 2020
On Saturday, 20 June 2020 at 13:57:38 UTC, Adam D. Ruppe wrote:On Saturday, 20 June 2020 at 11:09:12 UTC, aberba wrote:I glad it works that way. It was clear what was happening there.[...]In that proposal, i"" is a string BUILDER, not a string. So it returns an object you can get a string out of, or do other things with too. [...]
Jun 20 2020
On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:It's still too complicated. The simplest way: int i = 99; string a = "text " ~ i ~ "!"; writeln(a); // text 99!Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C' You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186
Jul 30 2020
On Thu, Jul 30, 2020 at 06:59:11PM +0000, zoujiaqing via Digitalmars-d wrote: [...]The simplest way: int i = 99; string a = "text " ~ i ~ "!"; writeln(a); // text 99!You could just use std.conv.text: int i = 99; string a = text("blah ", i, "!"); writeln(a); // blah 99! T -- MASM = Mana Ada Sistem, Man!
Jul 30 2020
On 7/30/20 2:59 PM, zoujiaqing wrote:On Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:what's complicated about: string a = i"text $i!".idup; -SteveOn Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:It's still too complicated. The simplest way: int i = 99; string a = "text " ~ i ~ "!"; writeln(a); // text 99!Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C' You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186
Jul 31 2020
On Friday, 31 July 2020 at 13:56:00 UTC, Steven Schveighoffer wrote:On 7/30/20 2:59 PM, zoujiaqing wrote:Thanks Steve. but about: string a = i"text $i!".idup; or: string a = "text" ~ i ~ "!"; or: string a = "text {$i}!"; I think the latter two are simpler ;) Ref: https://www.php.net/manual/en/language.types.string.phpOn Thursday, 18 June 2020 at 09:13:53 UTC, Dennis wrote:what's complicated about: string a = i"text $i!".idup; -SteveOn Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:It's still too complicated. The simplest way: int i = 99; string a = "text " ~ i ~ "!"; writeln(a); // text 99!Type should be automatically converted to string!While this could be made to work for floating point numbers, it is ambiguous for integers since integers are often implicitly convertible to characters: writeln("AB"~67); // prints "ABC", code unit 67 represents 'C' You might be interested in Adam's string interpolation DIP though: https://github.com/dlang/DIPs/pull/186
Jul 31 2020
On 7/31/20 2:18 PM, zoujiaqing wrote:On Friday, 31 July 2020 at 13:56:00 UTC, Steven Schveighoffer wrote:So we have 2 alternatives here. Both are viable, and both have precedent. And without getting into the entire discussion again, I can tell you why I would not want them. 1. string a = "text" ~ i ~ "!"; For this to work, the compiler has to call a function to process i into string form. This could technically be done for integers without allocations. But in the general case it cannot: string a = "text" ~ myStruct ~ "!"; Necessarily this has to be a conversion from the struct into a string, which then could be included in the concatenation. I'm not a fan of hidden allocations. I'd much rather see: string a = "text" ~ myStruct.to!string ~ "!"; 2. string a = "text {$i}!"; I'm sure you are aware that we will never see a feature just happen in existing strings like this. We absolutely need a marker for interpolated strings to distinguish interpolated strings from normal strings to prevent existing code breakage. So I'm going to refashion this as another oft-requested idea, borrowing the DIP syntax a bit: string a = i"text $i!"; The i prefix meaning "interpolated string". Such a request is also reasonable. However, it's too meh. D has so much potential power with lowering and compile-time processing, that it would be a waste to force string interpolation this way. Yes, an important use case is creating a string on the heap with interpolated items, and assigning to a string. But for the extra suffix ".idup", you do get that possibility. AND if your goal is NOT just to create a string, there are far more efficient and useful things that can be had by the proposal: writefln(i"text $i"); // just works auto row = db.selectRow(i"SELECT * FROM tbl1 WHERE col1 = $i and col2 = $j"); // i and j passed as parameters to avoid sql injection I WOULD be OK with an interpolated string automagically casting to a string, which converts into a library call for allocation. But this would really have to be something outside the normal type system. For avoiding the explicit ".idup" call, I think this is probably not worth it. And as Adam says in his DIP, the compiler can suggest using .idup when you should.what's complicated about: string a = i"text $i!".idup;Thanks Steve. but about: string a = i"text $i!".idup; or: string a = "text" ~ i ~ "!"; or: string a = "text {$i}!"; I think the latter two are simpler ;)Ref: https://www.php.net/manual/en/language.types.string.phpI use string interpolation in php all the time, mostly for SQL query writing. Super-prone to SQL injection, but I'm not going to change it now... -Steve
Aug 03 2020
On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:Now dlang string processing is complex, like this: ... Type should be automatically converted to string! ... Like java:No, it shouldn't. `string` is not a class, it's simply an array of immutable chars. To an array, you can only append an implicitly convertible type, or another array. You can use [1], [2], or, for this example's sake, [3] import std.stdio; import std.conv : text; import std.format : format; void main() { string name = "Brian"; uint age = 18; string str = text("My name is ", name, " and my age is ", age, "."); writeln(str); str = format("My name is %s and my age is %s.", name, age); writeln(str); writefln("My name is %s and my age is %s.", name, age); } [1] https://dlang.org/phobos/std_conv.html#text [2] https://dlang.org/phobos/std_format.html#.format [3] https://dlang.org/phobos/std_stdio.html#.writefln
Jun 18 2020
On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:It should be: ```D import std.stdio; void main() { string name = "Brian"; uint age = 18; string text = "My name is " ~ name ~ " and my age is " ~ age ~ "."; writeln(text); } ```You can use https://dlang.org/phobos/std_conv.html#text string s = text("My name is ", name, " and my age is ", age, ".");
Jun 18 2020
On Thursday, 18 June 2020 at 09:44:15 UTC, Kagamin wrote:You can use https://dlang.org/phobos/std_conv.html#text string s = text("My name is ", name, " and my age is ", age, ".");That looks actually quite nifty, I'll have to use that someday. Still it requires an import for std.conv, and when you import std.conv you might as well just to!string all the way. Imagine if using ~ required import std.concat, I am sure it will be used more sparingly than it is now.
Jun 18 2020
On Thursday, 18 June 2020 at 10:19:25 UTC, JN wrote:On Thursday, 18 June 2020 at 09:44:15 UTC, Kagamin wrote:You can just add a "import std" at the top of your module and stop worrying about imports.You can use https://dlang.org/phobos/std_conv.html#text string s = text("My name is ", name, " and my age is ", age, ".");That looks actually quite nifty, I'll have to use that someday. Still it requires an import for std.conv, and when you import std.conv you might as well just to!string all the way. Imagine if using ~ required import std.concat, I am sure it will be used more sparingly than it is now.
Jun 18 2020
On Thursday, 18 June 2020 at 10:19:25 UTC, JN wrote:On Thursday, 18 June 2020 at 09:44:15 UTC, Kagamin wrote:`text` function is shorter, it's close to interpolated string in ergonomics while not being interpolated string.You can use https://dlang.org/phobos/std_conv.html#text string s = text("My name is ", name, " and my age is ", age, ".");That looks actually quite nifty, I'll have to use that someday. Still it requires an import for std.conv, and when you import std.conv you might as well just to!string all the way.
Jun 18 2020
On 6/18/20 6:19 AM, JN wrote:On Thursday, 18 June 2020 at 09:44:15 UTC, Kagamin wrote:age.to!string will allocate a temporary string just to throw it away. text will not do that. -SteveYou can use https://dlang.org/phobos/std_conv.html#text string s = text("My name is ", name, " and my age is ", age, ".");That looks actually quite nifty, I'll have to use that someday. Still it requires an import for std.conv, and when you import std.conv you might as well just to!string all the way.
Jun 18 2020
On Thursday, 18 June 2020 at 13:59:42 UTC, Steven Schveighoffer wrote:age.to!string will allocate a temporary string just to throw it away. text will not do that. -SteveWouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Jun 18 2020
On 6/18/20 10:04 AM, Stanislav Blinov wrote:On Thursday, 18 June 2020 at 13:59:42 UTC, Steven Schveighoffer wrote:Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't. For sure format does not do this. (checks... yeah, it avoids that, but the code is a mess comparatively) -Steveage.to!string will allocate a temporary string just to throw it away. text will not do that. -SteveWouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Jun 18 2020
On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer wrote:On 6/18/20 10:04 AM, Stanislav Blinov wrote:Let's rephrase it then ;) https://issues.dlang.org/show_bug.cgi?id=20950On Thursday, 18 June 2020 at 13:59:42 UTC, Steven Schveighoffer wrote:Yikes. Let me rephrase.age.to!string will allocate a temporary string just to throw it away. text will not do that.Wouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Jun 18 2020
On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer wrote:On 6/18/20 10:04 AM, Stanislav Blinov wrote:It used to call std.conv.toChars but that triggered a dmd regression with a particular set of circumstances so it was changed to use to!(): https://github.com/dlang/phobos/pull/6659 In bugzilla there was a better alternative workaround but no one submitted a pull for that in time for the release: https://issues.dlang.org/show_bug.cgi?id=17712#c11 I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.https://github.com/dlang/phobos/blob/master/std/conv.d#L4193Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
Jun 19 2020
On 6/19/20 6:57 AM, Nick Treleaven wrote:On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer wrote:I think we should revert back to toChars, and see what the test suite produces. The test case should be added to the test suite as well. -SteveOn 6/18/20 10:04 AM, Stanislav Blinov wrote:It used to call std.conv.toChars but that triggered a dmd regression with a particular set of circumstances so it was changed to use to!(): https://github.com/dlang/phobos/pull/6659 In bugzilla there was a better alternative workaround but no one submitted a pull for that in time for the release: https://issues.dlang.org/show_bug.cgi?id=17712#c11 I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.https://github.com/dlang/phobos/blob/master/std/conv.d#L4193Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
Jun 19 2020
On Friday, 19 June 2020 at 12:06:36 UTC, Steven Schveighoffer wrote:On 6/19/20 6:57 AM, Nick Treleaven wrote:OK. I added the two test cases in the issue to dmd/test/compilable, then when that's merged we can revert: https://github.com/dlang/phobos/pull/7536I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.I think we should revert back to toChars, and see what the test suite produces. The test case should be added to the test suite as well.
Jun 19 2020
On Friday, 19 June 2020 at 10:57:42 UTC, Nick Treleaven wrote:It used to call std.conv.toChars but that triggered a dmd regression with a particular set of circumstances so it was changed to use to!(): https://github.com/dlang/phobos/pull/6659 In bugzilla there was a better alternative workaround but no one submitted a pull for that in time for the release: https://issues.dlang.org/show_bug.cgi?id=17712#c11 I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.Am I reading this right? Library was downgraded because of a regression in the compiler? Tell me I'm not reading this right.
Jun 19 2020
On 6/19/20 8:37 AM, Stanislav Blinov wrote:On Friday, 19 June 2020 at 10:57:42 UTC, Nick Treleaven wrote:I don't think it's a regression in the compiler, I think it's a bug that always existed in the compiler, and a change to phobos brought it out. So the decision was made to undo the phobos change rather than tackle the (probably very difficult) compiler bug. Not saying I agree with the decision, but that was why it was done. Some were not happy with the resolution. I wish the compiler could be fixed, but I lack the skills to even approach it. It seems the speculative generation of templates is a difficult problem for even regular D compiler devs to approach. -SteveIt used to call std.conv.toChars but that triggered a dmd regression with a particular set of circumstances so it was changed to use to!(): https://github.com/dlang/phobos/pull/6659 In bugzilla there was a better alternative workaround but no one submitted a pull for that in time for the release: https://issues.dlang.org/show_bug.cgi?id=17712#c11 I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.Am I reading this right? Library was downgraded because of a regression in the compiler? Tell me I'm not reading this right.
Jun 19 2020
On Friday, 19 June 2020 at 10:57:42 UTC, Nick Treleaven wrote:On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer wrote:I reduced it a but, but then it started to depend on the order of operations: fail: string test1() { import std.uni : CodepointSet; import std.conv : to; uint a; CodepointSet charsets; string code = to!string(a); charsets.toSourceCode; return code; } success: string test1() { import std.uni : CodepointSet; import std.conv : to; uint a; CodepointSet charsets; charsets.toSourceCode; string code = to!string(a); return code; } Looks like it couldn't compare template instances well.On 6/18/20 10:04 AM, Stanislav Blinov wrote:It used to call std.conv.toChars but that triggered a dmd regression with a particular set of circumstances so it was changed to use to!(): https://github.com/dlang/phobos/pull/6659 In bugzilla there was a better alternative workaround but no one submitted a pull for that in time for the release: https://issues.dlang.org/show_bug.cgi?id=17712#c11 I haven't been able to reproduce the test case with v2.092.0, but maybe I'm doing something wrong.https://github.com/dlang/phobos/blob/master/std/conv.d#L4193Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
Jun 19 2020
Reduced std.uni --- test3 --- string test1() { import test3a; import std.conv : to; uint a; //B.c(); string s = to!string(a); B.c(); return s; } int main(){ return 0; } --- test3a --- alias B = A!int; template A(T) { string c() { import std.format : format; return format("%s",""); } } ---
Jun 20 2020
On Thursday, 18 June 2020 at 09:00:42 UTC, zoujiaqing wrote:Now dlang string processing is complex, like this:We've talked too much on this topic, I've found a number of DIPs, some string interpolation dub packages, either no-longer maintained, or does not fit my need. So I decided to just-do-it: https://code.dlang.org/packages/jdiutil jdiutil: Just-Do-It util mixin Some small util mixin to make (debug) life easier: -- string interpolation for easy debug print: _S with var name; _s without var name -- ToString will generate string with class fields content, instead of just plain pointer. -- ReadOnly, ReadWrite declare fields without boilerplate code -- Singleton, Low-Lock Singleton Pattern https://wiki.dlang.org/Low-Lock_Singleton_Pattern -- AtomicCounted, atomic counter Examples, check app.d: -------------------------------- public import jdiutil; class Point { // declare fields mixin ReadOnly! (int, "x"); mixin ReadWrite!(double, "y"); mixin ReadWrite!(string, "label", "default value"); // atomic counter mixin AtomicCounted; // this is a Singleton class! mixin Singleton!Point; // debug print string helper mixin ToString!Point; } void main() { int i = 100; double d = 1.23456789; Point thePoint = Point.getSingleton(); // multiple vars separated by ';' // _S with var name; _s without var name writeln(mixin(_S!"print with var name: {i; d; thePoint}")); writeln(mixin(_s!"print without var name: {i; d; thePoint}")); thePoint.incCount(); logger.info(mixin(_S!"works in logger too: {i; d; thePoint}")); thePoint.incCount(); string str = mixin(_S!"assign to string with custom format: {i; d%06.2f; thePoint}"); writeln(str); } -------------------------------- Output: -------------------------------- print with var name: i=100 d=1.23457 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=0) print without var name: 100 1.23457 app.Point(_x=0 _y=nan _label=default value _counter=0) 2020-06-20T22:31:29.053 [info] app.d:38:main works in logger too: i=100 d=1.23457 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=1) assign to string with custom format: i=100 d=001.23 thePoint=app.Point(_x=0 _y=nan _label=default value _counter=2) -------------------------------- This is my 1st dub package, suggestions welcome (please use the github issue, easier to track.) Thanks for everyone on this forum who have helped!
Jun 20 2020