Cannot cast char[] to string.

Brad Anderson eco at gnuk.net
Thu Nov 14 11:45:55 PST 2013


On Thursday, 14 November 2013 at 19:41:13 UTC, Agustin wrote:
> I'm trying to use http://dlang.org/phobos/std_net_curl.html and 
> when i compile the same example i get:
>
> cannot implicitly convert expression 
> (get(cast(const(char)[])address, AutoProtocol())) of type 
> char[] to string
>
> string address = "http://dlang.org";
> string _data = get(address);

You have two options:

string address = "http://dlang.org";
string _data = get(address).idup(); // create immutable copy

or

string address = "http://dlang.org";
char[] _data = get(address); // store mutable reference

A string (which is just an alias of immutable(char)[]) can't be 
made from a char[] without an assertion that the data pointed to 
is, in fact, immutable.  You can do that using assumeUnique 
(inexplicably found in std.exception).


More information about the Digitalmars-d-learn mailing list