missing HexString documentation

Walter Bright newshound2 at digitalmars.com
Thu Feb 8 20:49:09 UTC 2018


On 2/8/2018 10:42 AM, Steven Schveighoffer wrote:
> On 2/8/18 1:25 PM, Walter Bright wrote:
> "abc" is an array (it's an immutable(char)[]). There's no reason why 
> ['a','b','c'] should be different than "abc" (other than the hidden null 
> character, which is irrelevant here).

['a','b','c'] is mutable, a string literal is immutable.


> Perhaps the fact that using a string rather than an array causes code to fail 
> should be addressed?

That would be a language change proposal or bug report. By all means, please do so.


> How can this be a huge simplification? I mean you already have code that parses 
> hex characters in a string array, all you need is one flag that assumes all 
> character pairs have been preceded by \x. I think this will save probably 4 or 5 
> lines of code?

hexStringConstant() was 79 lines of code, not including comments and blank lines.

I also showed how:

    x"deadbeef"

can be replaced with:

    hexString!"deadbeef"

with no overhead. If you hate typing hexString, you can always write:

    alias x = hexstring;

and then you've got:

    x"deadbeef"
    x!"deadbeef"

which seems an inconsequential difference. (The generated code is the same.)


> It also doesn't preclude at all someone writing library code to make their own 
> custom string syntax.

You're right it doesn't. But people don't do it, because it is neither obvious 
that D can do such a thing (it relies on a combination of features) nor is it 
obvious how to do it correctly (as the earlier hexString implementation shows 
and nobody seemed able to fix it but me). What Phobos provides is working, 
professional quality code that should serve as a user resource for "how to do 
things and how to do them right".

I.e. having hexString as a library function is a good advertisement for what D 
can do. After all, how many languages can do this sort of thing?


More information about the Digitalmars-d mailing list