Implicit conversion of char[] to string struct

Alan Smith alanrogersmith at gmail.com
Sun Dec 30 08:02:51 PST 2007


Hi,

I would like to know if it is possible to convert a char[] to a struct that has a opCall(char[] str) function. For example:

import std.stdio;

char firstCharacterInString(pstring str)
{
    return str[0];
}


void main()
{
    writefln("first char = '%s'", firstCharacterInString("Hello World!")); // should print "first char = 'H'"
}

pstring = http://www.dprogramming.com/dstring.php

The above code will output something like this:

Error: cannot implicitly convert expression ("Hello World!") of type char[12u] to pstring

Is there any way to make D substitute "Hello World!" for pstring("Hello World!")? How do I get the above code to work without creating an instance of pstring like this:

pstring str = "Hello World!";

writefln("first char = '%s'", firstCharacterInString(str));

All help is appreciated!

Peace, Alan


More information about the Digitalmars-d-learn mailing list