Regarding hex strings

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Oct 18 16:51:33 PDT 2012


On Wed, 17 Oct 2012 19:49:43 -0700
"H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote:

> 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.
> 

Big +1

Having the language expect x"..." to always be a string (let alone a
*valid UTF* string) is just insane. It's just too damn useful for
arbitrary binary data.




More information about the Digitalmars-d mailing list