string from C function - to!string

Nick Treleaven nick at geany.org
Wed May 14 11:38:59 UTC 2025


On Wednesday, 14 May 2025 at 03:36:40 UTC, Jonathan M Davis wrote:
> to!string definitely deals with null-terminated strings, or it 
> wouldn't work at all. It's not the kind of thing that would 
> work by accident.

I don't think it's good API design:

> Pointer to string conversions convert the pointer to a size_t 
> value. If pointer is char*, treat it as C-style strings. In 
> that case, this function is @system.

So `to!string` is bad for generic code. Want to represent the 
address of a byte* as hex in a string, fine. Oh now the pointer's 
element type is char - wait, why isn't the result hex any more?

I really dislike overloads having different behaviour like this 
and it would be nice if Phobos 3 could take a much stricter 
stance on that.

On the docs:

This special behaviour is buried as *part* of a single bullet 
point in long documentation that is not easy to find. (And 
there's currently no anchor link for that section - each 
'Examples:' section should probably have one).


More information about the Digitalmars-d-learn mailing list