D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2193 - Allow implicit conversions: char -> ubyte, wchar -> ushort, dchar -> uint
Summary: Allow implicit conversions: char -> ubyte, wchar -> ushort, dchar -> uint
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2008-07-05 05:33 UTC by Matti Niemenmaa
Modified: 2024-12-13 17:48 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Matti Niemenmaa 2008-07-05 05:33:21 UTC
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.
Comment 1 bearophile_hugs 2013-02-22 09:37:18 UTC
Now the signatures of std.string.toStringz are:

pure nothrow immutable(char)* toStringz(const(char)[] s);
pure nothrow immutable(char)* toStringz(string s);

Consider is it's worth keeping this issue open.
Comment 2 dlangBugzillaToGithub 2024-12-13 17:48:37 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17739

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB