Jump to page: 1 2 3
Thread overview
About string and int spliced together.
Jun 18, 2020
zoujiaqing
Jun 18, 2020
Dennis
Jun 18, 2020
zoujiaqing
Jun 19, 2020
Nick Treleaven
Jun 20, 2020
aberba
Jun 20, 2020
Adam D. Ruppe
Jun 20, 2020
aberba
Jul 30, 2020
zoujiaqing
Jul 30, 2020
H. S. Teoh
Jul 31, 2020
zoujiaqing
Jun 18, 2020
Stanislav Blinov
Jun 18, 2020
Kagamin
Jun 18, 2020
JN
Jun 18, 2020
Seb
Jun 18, 2020
Kagamin
Jun 18, 2020
Stanislav Blinov
Jun 18, 2020
Stanislav Blinov
Jun 19, 2020
Nick Treleaven
Jun 19, 2020
Nick Treleaven
Jun 19, 2020
Stanislav Blinov
Jun 20, 2020
Kagamin
Jun 20, 2020
Kagamin
Jun 21, 2020
mw
June 18, 2020
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 + ".");
```



June 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
June 18, 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
June 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:
>> 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!
June 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, ".");
June 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.
June 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 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.
June 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 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.
June 18, 2020
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
June 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.
>
> -Steve

Wouldn't it?

https://github.com/dlang/phobos/blob/master/std/conv.d#L4193
« First   ‹ Prev
1 2 3