[Issue 2193] New: Allow implicit conversions: char -> ubyte, wchar -> ushort, dchar -> uint

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jul 5 03:33:22 PDT 2008


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

           Summary: Allow implicit conversions: char -> ubyte, wchar ->
                    ushort, dchar -> uint
           Product: D
           Version: 1.031
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: matti.niemenmaa+dbugzilla at iki.fi


As it stands it is quite impossible to use an API which is strict about keeping
char as UTF-8 only. For instance, given a function which turns a C string into
a D string:

// accepts anything, not only UTF-8, hence ubyte and not char
ubyte[] fromStringz(ubyte*);

One can't call it with a char*, even though the function itself would work
fine. Casting works, of course, but the end result of such is that code starts
to look something like the following:

auto x = getSomeUTF8();
auto y = &x[5];
x = cast(char[])foo(cast(ubyte*)y);
bar(cast(ubyte[])x);
return cast(ubyte)x[0];

This is far too verbose and unreadable. The only "real" cast there is the
cast(char[]) which asserts that foo, given UTF-8, returns valid UTF-8. The rest
are essentially just saying that "yes, UTF-8 bytes are the same size as
ubytes!", which should not be necessary.

The wchar->ushort and dchar->uint conversions should be included for
completeness's sake, but I suspect they aren't as necessary.


-- 



More information about the Digitalmars-d-bugs mailing list