Thread overview
type conversions
Apr 29, 2012
WhatMeWorry
Apr 30, 2012
Era Scarecrow
Apr 30, 2012
Andrej Mitrovic
Apr 30, 2012
Jonathan M Davis
Apr 30, 2012
Ary Manzana
Apr 30, 2012
Jonathan M Davis
April 29, 2012
I'm trying to get my head around D's type conversion. What is the
best way to convert a string to a char array? Or I should say is
this the best way?

string s = "Hello There";
char[] c;

c = string.dup;


Also, what is the best way to explicitly convert a string to an
int?  I've been looking at Library Reference (Phobos) but I'm
stuck.


April 30, 2012
On Sunday, 29 April 2012 at 23:42:39 UTC, WhatMeWorry wrote:
> I'm trying to get my head around D's type conversion. What is the
> best way to convert a string to a char array? Or I should say is
> this the best way?
>
> string s = "Hello There";
> char[] c;
>
> c = string.dup;
>
>
> Also, what is the best way to explicitly convert a string to an
> int?  I've been looking at Library Reference (Phobos) but I'm
> stuck.

I think this is right...

import std.stdio;
import std.conv;

int main(string args[]){
	string str = "Hello world!";
	char[] c = to!(char[])(str);
	
	string num = "1234";
	int i = to!int(num);
	
	writefln("%s\t- %s", typeid(str), str);
	writefln("%s\t- %s", typeid(num), num);

	writefln("%s\t- %s", typeid(c), c);
	writefln("%s\t- %s", typeid(i), i);

return 0;
}

April 30, 2012
On 4/30/12, WhatMeWorry <kc_heaser@yahoo.com> wrote:
> I'm trying to get my head around D's type conversion. What is the best way to convert a string to a char array? Or I should say is this the best way?
>
> string s = "Hello There";
> char[] c;
>
> c = string.dup;
>

Well it depends . Why do you need a char[]? If you're interfacing with C you most likely need a 0-terminated string and not a char[].

> Also, what is the best way to explicitly convert a string to an int?

import std.conv;
int i = to!int("12345");
April 30, 2012
On Monday, April 30, 2012 01:42:38 WhatMeWorry wrote:
> I'm trying to get my head around D's type conversion. What is the best way to convert a string to a char array? Or I should say is this the best way?
> 
> string s = "Hello There";
> char[] c;
> 
> c = string.dup;

dup will return a mutable copy of an array. idup will return an immutable copy of an array. They will both always copy. If you want to convert without having to make a copy if the array is of the constancy that you want already (e.g. if a templated function is templated on string type, and it could be any constancy of char[]), then use std.conv.to.

auto c = to!(char[])(str);

If str was already char[], then it will just be returned, whereas if it's immutable(char)[], then it would dup it and return that.

> Also, what is the best way to explicitly convert a string to an int?  I've been looking at Library Reference (Phobos) but I'm stuck.

Use std.conv.to:

auto i = to!string("1234");

std.conv.to is what you use for pretty much any conversion.

- Jonathan M Davis
April 30, 2012
On 4/30/12 8:08 AM, Jonathan M Davis wrote:
> On Monday, April 30, 2012 01:42:38 WhatMeWorry wrote:
>> I'm trying to get my head around D's type conversion. What is the
>> best way to convert a string to a char array? Or I should say is
>> this the best way?
>>
>> string s = "Hello There";
>> char[] c;
>>
>> c = string.dup;
>
> dup will return a mutable copy of an array. idup will return an immutable copy
> of an array. They will both always copy. If you want to convert without having
> to make a copy if the array is of the constancy that you want already (e.g. if
> a templated function is templated on string type, and it could be any
> constancy of char[]), then use std.conv.to.
>
> auto c = to!(char[])(str);
>
> If str was already char[], then it will just be returned, whereas if it's
> immutable(char)[], then it would dup it and return that.
>
>> Also, what is the best way to explicitly convert a string to an
>> int?  I've been looking at Library Reference (Phobos) but I'm
>> stuck.
>
> Use std.conv.to:
>
> auto i = to!string("1234");
>
> std.conv.to is what you use for pretty much any conversion.
>
> - Jonathan M Davis

Can the documentation of std.conv be fixed?

http://dlang.org/phobos/std_conv.html#to

I mean, all the toImpl methods are documented, but in "to" it clearly says "Client code normally calls to!TargetType(value) (and not some variant of toImpl." I think all the documentation should be in "to". Now it sounds like you know what "to" does... but people read documentation because they don't know what it does.

There's also no need to document all the different parse methods in different places. Just one place is enough and simpler to read.
April 30, 2012
> Can the documentation of std.conv be fixed?
> 
> http://dlang.org/phobos/std_conv.html#to
> 
> I mean, all the toImpl methods are documented, but in "to" it clearly says "Client code normally calls to!TargetType(value) (and not some variant of toImpl." I think all the documentation should be in "to". Now it sounds like you know what "to" does... but people read documentation because they don't know what it does.

The comment on to could obviously be changed, but it wouldn't be all that fun to try and not have toImpl in the documentation. You'd have to do something like

version(StdDdoc)
{
 /++
 ddoc comment
 +/
 T to(T, S)(S value)
 if(...)
 {assert(0);}
}
else
{
 T toImpl(T, S)(S Value)
 if(...)
 {...}
}

for every toImpl, which would create undesirable code duplication on top of being ugly. You can certainly make the changes and create a pull request for them if you want to though. Another approach would simply be to put better examples in the documentation.

The weird thing is that there's a huge comment with a variety of examples near the top of the file that looks an awful lot like it's intended to be the ddoc comment for the module, but it's not actually at the top of the module, nor is it on any symbol (and it specifically has a space after its first asterisk so that it's not a ddoc comment). It's also, right above to, so maybe it was intended to be on to at one point. Either way, it's a bit weird.

- Jonathan M Davis