std.digest toHexString

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 16 10:20:45 PDT 2017


On Thu, Mar 16, 2017 at 04:59:40PM +0000, Adam D. Ruppe via Digitalmars-d-learn wrote:
> 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.
[...]

I'm not convinced casting static array to immutable is OK. Check this
out:

	import std.stdio;

	char[32] func() {
	    char[32] staticArr = "A123456789abcdefB123456789abcdef";
	    return staticArr; // OK, by-value return
	}

	string gunk() {
	    string x = func(); // implicit conversion char[32] -> string
	    writeln(x.ptr);
	    writeln(x);		// prints "A123456789abcdefB123456789abcdef"
	    return x;
	}

	void main() {
	    auto s = gunk();
	    writeln(s.ptr);	// prints same address as in gunk()
	    writeln(s);		// prints corrupted string
	}

Run this code and you'll see that s.ptr has the same address as x.ptr,
and that x.ptr is the address of a local variable. This is blatantly
wrong.

Filed a new issue for this:

	https://issues.dlang.org/show_bug.cgi?id=17261


T

-- 
I am a consultant. My job is to make your job redundant. -- Mr Tom


More information about the Digitalmars-d-learn mailing list