www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11103] New: w and d suffix for char literals too

http://d.puremagic.com/issues/show_bug.cgi?id=11103

           Summary: w and d suffix for char literals too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-09-22 14:57:28 PDT ---
A low priority enhancement request.

String literals support the c w d suffix to specify their type:

void main() {
    auto s1 = "hello"c;
    auto s2 = "hello"w;
    auto s3 = "hello"d;
}


For wchars/dchars you could use a cast:

void main() {
    auto c1 =            'X';
    auto c2 = cast(wchar)'X';
    auto c3 = cast(dchar)'X';
}


But I suggest to support the same string suffixes:

void main() {
    auto c1 = 'X';
    auto c2 = 'X'w;
    static assert(is(typeof(c2) == wchar));
    auto c3 = 'X'd;
    static assert(is(typeof(c3) == dchar));    
}


This has some advantages:
- It's shorter than a cast, it takes only 1 extra char.
- During debugging and in other situations I search for the "cast(" string in
my code, because sometimes casts are where bugs are. Removing the need to use
cast() for dchars/wchars removes some noise from that search.
- Those suffixes are not hard to learn for a D programmer because they are the
same for strings. And I think this change is backwards compatible.

Disadvantages:
- It's not a very common need;
- Unlike the situation with strings where you can't use a cast, the
cast(wchar)/cast(dchar) work fine on chars.


One use case:

import std.algorithm: map, joiner;
import std.string: text;
void main() {
    string r = [1, 2]
               .map!(x => [1, 2].map!(y => cast(dchar)'*'))
               .joiner("_")
               .text;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 22 2013