Implicit string lit conversion to wstring/dstring

Alex Rønne Petersen xtzgzorex at gmail.com
Wed Mar 14 11:02:01 PDT 2012


On 14-03-2012 19:00, Alex Rønne Petersen wrote:
> On 14-03-2012 19:00, H. S. Teoh wrote:
>> Code:
>> import std.stdio;
>> version(explicit) {
>> void func(dstring s) {
>> dstring t = s;
>> writeln(t);
>> }
>> } else {
>> void func(S)(S s) {
>> dstring t = s; // line 10
>> writeln(t);
>> }
>> }
>> void main() {
>> func("abc");
>> }
>>
>> If version=explicit is set, the program compiles fine. But if not, then
>> the compiler complains:
>>
>> test.d:10: Error: cannot implicitly convert expression (s) of type
>> string to immutable(dchar)[]
>>
>> What do I need to do to make the template version of func trigger
>> implicit conversion of the string lit to dstring? What I'm trying to do
>> is to templatize dstring as well, but still benefit from the
>> string->dstring conversion when instantiated with dstring.
>>
>> (Ditto with implicit conversion to wstring.)
>>
>>
>> T
>>
>
> I doubt that such an implicit conversion even exists. You have to prefix
> the string appropriately, e.g.:
>
> string s = "foo";
> wstring w = w"bar";
> dstring d = d"baz";
>

Sorry, I lied. Like this:

string s = "foo"c; (the c is optional)
wstring w = "bar"w;
dstring d = "baz"d;

See: http://dlang.org/lex.html

-- 
- Alex


More information about the Digitalmars-d mailing list