Regarding hex strings

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Oct 17 19:49:43 PDT 2012


On Thu, Oct 18, 2012 at 02:45:10AM +0200, bearophile wrote:
[...]
> hex strings are useful, but I think they were invented in D1 when
> strings were convertible to char[]. But today they are an array of
> immutable UFT-8, so I think this default type is not so useful:
> 
> void main() {
>     string data1 = x"A1 B2 C3 D4"; // OK
>     immutable(ubyte)[] data2 = x"A1 B2 C3 D4"; // error
> }
> 
> 
> test.d(3): Error: cannot implicitly convert expression
> ("\xa1\xb2\xc3\xd4") of type string to ubyte[]
[...]

Yeah I think hex strings would be better as ubyte[] by default.

More generally, though, I think *both* of the above lines should be
equally accepted.  If you write x"A1 B2 C3" in the context of
initializing a string, then the compiler should infer the type of the
literal as string, and if the same literal occurs in the context of,
say, passing a ubyte[], then its type should be inferred as ubyte[], NOT
string.


T

-- 
Who told you to swim in Crocodile Lake without life insurance??


More information about the Digitalmars-d mailing list