Bug 5909 – Allow implicit conversion of hex strings to immutable(ubyte)[] and immutable(ubyte[N]) or turn them into ubyte array literals
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-29T09:10:59Z
Last change time
2020-08-22T20:07:30Z
Assigned to
No Owner
Creator
kennytm
Comments
Comment #0 by kennytm — 2011-04-29T09:10:59Z
As titled.
Some string features like \x escape characters and x"" string may produce an invalid UTF string. This may not be useful for the 'string' type, but is very useful for creating a 'ubyte[]' type which isn't limited by the character set.
Unfortunately, correctly a string literal can only be implicitly converted to an immutable char/wchar/dchar pointer/array, so the following will not work without a cast:
-------------------------------
void main() {
immutable(ubyte)[] a = x"1a b2 3c d4 5e f6";
}
-------------------------------
$ dmd x
x.d(2): Error: cannot implicitly convert expression ("\x1a\xb2<\xd4^\xf6") of type string to immutable(ubyte)[]
-------------------------------
(See also issue 2193 for a generalization.)
Comment #1 by yebblies — 2014-01-19T07:57:52Z
*** Issue 10453 has been marked as a duplicate of this issue. ***
Comment #2 by bearophile_hugs — 2014-01-22T16:42:42Z
Hit this problem again. Issue 10453 shows evidence that even Phobos needs this feature. I consider this almost a bug report instead of just an enhancement request.
Comment #3 by bearophile_hugs — 2014-01-22T16:45:50Z
Fixed the issue title.
See also Issue 10454
Comment #4 by bearophile_hugs — 2014-05-19T12:00:35Z
If implicit conversions are not appreciated, then an alternative solution is to consider hex strings as ubyte array literals:
pragma(msg, typeof(x"1a b2"));
==>
immutable(ubyte)[]
This avoids implicit conversions. In 99% of the cases you don't want that literal to produce a string, so this is useful.