std.digest toHexString

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 16 09:59:40 PDT 2017


On Thursday, 16 March 2017 at 16:47:14 UTC, Carl Sturtivant wrote:
> Silently <expletive-deleted> cast to immutable without 
> copying!??!! This is so wrong.

It is the implicit slicing that I really want removed from the 
language, it serves no real benefit and brings a lot of 
accidental complexity. Casting the static array to immutable is 
actually OK in isolation, because it is a value type and thus a 
unique copy... but once you slice it, that promise is gone.

> Yet the documentation says there's a toHexString that returns a 
> string.

Yes, indeed, it returns a string if it is passed a dynamic array.

Remember though, like I warned on my doc fork, overload 
resolution NEVER looks at the left hand side of the equation, it 
is always done on arguments alone.

The stupid auto return stuff Phobos loves so much obscures it, 
but md5Of returns a ubyte[16].

That calls the first overload, the one that returns char[num*2], 
since the argument most strongly matches the static array one. 
(Interestingly, if it didn't exist, the language would probably 
idiotically slice that ubyte[16] into a ubyte[] and we'd get 
right behavior, for the wrong reason. The implementation of the 
slice one does `new char[]` inside.)

If you explicitly sliced it before the call,

toHexString(md5Of(...)[]); // notice the []

then it would call the one that returns the string and be fine!



More information about the Digitalmars-d-learn mailing list