type conversions

Ary Manzana ary at esperanto.org.ar
Mon Apr 30 01:56:00 PDT 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.


More information about the Digitalmars-d-learn mailing list