www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - About string and int spliced together.

reply zoujiaqing <zoujiaqing gmail.com> writes:
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
next sibling parent reply Dennis <dkorpel gmail.com> writes:
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
next sibling parent zoujiaqing <zoujiaqing gmail.com> writes:
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!
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
Yes! You are right! Thanks!
Jun 18
prev sibling next sibling parent Nick Treleaven <nick geany.org> writes:
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!
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'
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=18346
Jun 19
prev sibling parent reply aberba <karabutaworld gmail.com> writes:
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/186
So 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
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent aberba <karabutaworld gmail.com> writes:
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:
 [...]
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. [...]
I glad it works that way. It was clear what was happening there.
Jun 20
prev sibling next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
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
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
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
parent reply JN <666total wp.pl> writes:
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
next sibling parent Seb <seb wilzba.ch> writes:
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 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.
You can just add a "import std" at the top of your module and stop worrying about imports.
Jun 18
prev sibling next sibling parent Kagamin <spam here.lot> writes:
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 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.
`text` function is shorter, it's close to interpolated string in ergonomics while not being interpolated string.
Jun 18
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/18/20 6:19 AM, JN wrote:
 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.
age.to!string will allocate a temporary string just to throw it away. text will not do that. -Steve
Jun 18
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
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.

 -Steve
Wouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Jun 18
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/18/20 10:04 AM, Stanislav Blinov wrote:
 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.

 -Steve
Wouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
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) -Steve
Jun 18
next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer 
wrote:
 On 6/18/20 10:04 AM, Stanislav Blinov wrote:
 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.
Wouldn't it? https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Yikes. Let me rephrase.
Let's rephrase it then ;) https://issues.dlang.org/show_bug.cgi?id=20950
Jun 18
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer 
wrote:
 On 6/18/20 10:04 AM, Stanislav Blinov wrote:
 https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
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.
Jun 19
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/19/20 6:57 AM, Nick Treleaven wrote:
 On Thursday, 18 June 2020 at 14:24:40 UTC, Steven Schveighoffer wrote:
 On 6/18/20 10:04 AM, Stanislav Blinov wrote:
 https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
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.
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. -Steve
Jun 19
parent Nick Treleaven <nick geany.org> writes:
On Friday, 19 June 2020 at 12:06:36 UTC, Steven Schveighoffer 
wrote:
 On 6/19/20 6:57 AM, Nick Treleaven wrote:
 I 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.
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/7536
Jun 19
prev sibling next sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
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
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/19/20 8:37 AM, Stanislav Blinov wrote:
 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.
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. -Steve
Jun 19
prev sibling parent reply Kagamin <spam here.lot> writes:
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:
 On 6/18/20 10:04 AM, Stanislav Blinov wrote:
 https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
Yikes. Let me rephrase. text is perfectly capable of avoiding this, and should, but for some reason doesn't.
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.
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.
Jun 19
parent Kagamin <spam here.lot> writes:
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
prev sibling parent mw <mingwu gmail.com> writes:
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