How should to!string handle infinite ranges?
Dukc
ajieskola at gmail.com
Wed Mar 5 14:25:16 UTC 2025
On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus wrote:
> As a D programmer, which behavior would you find more useful or
> less surprising?
>
> [1]: https://github.com/dlang/phobos/pull/10660
Good question.
On the one hand, to!string can be thought as a conversion of any
type to human-readable format, in which case infinite ranges
should probably be printed in truncated form as suggested.
On the other hand, a string is an array of characters. If I
attempt converting a range of characters to an array of
characters, my first instinct would be to expect a compiler error
if my range is infinite, as I would when when converting any
range to an array of the same type.
I tend to think the second interpretation is better in this case.
`to` is a general purpose conversion function, it's type argument
can be an array of anything. Therefore when it takes an array of
characters, it makes sense to treat it like any other array the
user asks to convert to.
I'm not sure if `to` will actually convert arbitrary ranges to
arrays like `array` does, but even if that's not the case it's
how I would expect it to behave _if_ it does - and it does in
case of a range of characters.
More information about the Digitalmars-d
mailing list