Template overloads involving `string` and `char[constant]` return value

Johan Engelen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 21 05:39:57 PDT 2016


On Wednesday, 21 September 2016 at 12:20:14 UTC, Adam D. Ruppe 
wrote:
>
> This is a pretty common pitfall (and IMO one of the most 
> egregious design flaws in the language), I see it all the time.

I write very little D code, so I guess it had to happen at some 
point then. Man, this is really bad :((

> toHexString, when given a static array, returns a static array, 
> but the language allows you to implicitly slice that into a 
> pointer (mistaken design in any case, doubly so since it a 
> stack pointer)... and moreover it is implicitly cast to 
> immutable!

Thanks for the explanation.
It should really be mentioned in the documentation of 
toHexString, with an actual example instead of a unittest.
The original code was doing:
```
     MD5 md5;
     md5.start();
     md5.put(cast(const(ubyte)[]) "some interesting data");
     auto hash = md5.finish();
     return toHexString!(LetterCase.lower)(hash);
```

I guess `ubyte[] hash = md5.finish();` would have fixed it too.


More information about the Digitalmars-d-learn mailing list