Thread overview
[Style] Converting from char[] to string, to!string vs idup
Mar 25, 2014
Mark Isaacson
Mar 25, 2014
Justin Whear
Mar 25, 2014
Mark Isaacson
Mar 25, 2014
bearophile
Mar 25, 2014
monarch_dodra
Mar 25, 2014
H. S. Teoh
March 25, 2014
I am presently working my way through TDPL for the second time, and there's an example in chapter 1 to the effect of:

[code]
string currentParagraph;
foreach(line; stdin.byLine()) {
  if (line.length > 2) {
    currentParagraph = to!string(line[2 .. $]);
  }
}
[/code]

The explicit conversion is necessary because `line` is a `char[]` but `currentParagraph` is a `string`. My question is why use `to!string` instead of just doing `line[2 .. $].idup`?

Is there a performance benefit? Is it simply because it's more general?

I tried taking a peek at the implementation of to!string, but it wasn't easy enough to follow and boil down into components.
March 25, 2014
On Tue, 25 Mar 2014 21:35:46 +0000, Mark Isaacson wrote:

> I am presently working my way through TDPL for the second time, and there's an example in chapter 1 to the effect of:
> 
> [code]
> string currentParagraph;
> foreach(line; stdin.byLine()) {
>    if (line.length > 2) {
>      currentParagraph = to!string(line[2 .. $]);
>    }
> }
> [/code]
> 
> The explicit conversion is necessary because `line` is a `char[]`
> but `currentParagraph` is a `string`. My question is why use `to!string`
> instead of just doing `line[2 .. $].idup`?
> 
> Is there a performance benefit? Is it simply because it's more general?
> 
> I tried taking a peek at the implementation of to!string, but it wasn't easy enough to follow and boil down into components.

I believe char[] -> string conversion with to! will use this implementation: https://github.com/D-Programming-Language/phobos/blob/ master/std/conv.d#L823

So there should be no performance implications, but to!string is better self-documenting and flexible should you need to change the input type in future.
March 25, 2014
Mark Isaacson:

> why use `to!string` instead of just doing `line[2 .. $].idup`?

I sometimes prefer the text function:

 = line[2 .. $].text;

Bye,
bearophile
March 25, 2014
On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:
> Is there a performance benefit? Is it simply because it's more general?

Mostly because it's more generic. For example:
If instead you want to do "string" => "char[]", then your code will have to be changed to use "dup".
if instead you want to do char[] => dstring, then your code will simply not work at all.

By contrast, "to!" will "just work". It's "A one-stop shop for converting values from one type to another."

There is *1* thing you should take into account though: "to!" is a no-op for string=>string or char[]=>char[], or anything else that can be implicitly converted as such. In contrast, "dup"/"idup" will create an actual copy.

Not that this is good or bad. Just something you should keep in mind.
March 25, 2014
Much appreciated everyone! I had a vague intuition that these were the reasons, but it was helpful to spell them out. I'm especially partial to the self-documentation reasoning.
March 25, 2014
On Tue, Mar 25, 2014 at 10:02:33PM +0000, monarch_dodra wrote:
> On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:
> >Is there a performance benefit? Is it simply because it's more general?
[...]
> There is *1* thing you should take into account though: "to!" is a no-op for string=>string or char[]=>char[], or anything else that can be implicitly converted as such. In contrast, "dup"/"idup" will create an actual copy.
> 
> Not that this is good or bad. Just something you should keep in mind.

I think it's a good thing. It avoids needless copying where it's not necessary. (Of course, if you have a char[] and you actually want a new copy, then you have to use .dup explicitly.)


T

-- 
Маленькие детки - маленькие бедки.
March 25, 2014
On Tue, 25 Mar 2014 19:13:07 -0400, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:

> On Tue, Mar 25, 2014 at 10:02:33PM +0000, monarch_dodra wrote:
>> On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:
>> >Is there a performance benefit? Is it simply because it's more general?
> [...]
>> There is *1* thing you should take into account though: "to!" is a
>> no-op for string=>string or char[]=>char[], or anything else that can
>> be implicitly converted as such. In contrast, "dup"/"idup" will create
>> an actual copy.
>>
>> Not that this is good or bad. Just something you should keep in mind.
>
> I think it's a good thing. It avoids needless copying where it's not
> necessary. (Of course, if you have a char[] and you actually want a new
> copy, then you have to use .dup explicitly.)

In the case of char[] -> char[], you explicitly want to dup. The original text is part of a buffer that is reused.

-Steve