How should to!string handle infinite ranges?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Mar 4 19:07:21 UTC 2025


On Tuesday, March 4, 2025 3:38:24 AM MST Paul Backus via Digitalmars-d wrote:
> We're currently having a discussion about this [on Github][1].
> The way I see it, there are two reasonable choices:
>
> 1. Return a finite string indicating that the range was infinite.
> For example, `"[1, 2, 3, ...]"`.
>
> 2. Fail to compile and print a meaningful error message using
> `static assert`. For example, `Can't convert an infinite range to
> a string`.
>
> As a D programmer, which behavior would you find more useful or
> less surprising?
>
> [1]: https://github.com/dlang/phobos/pull/10660

Ideally, what it would be doing is failing to compile due to the template
constraint failing, because it doesn't make sense to print an infinite
range. That's how infinite ranges are normally dealt with. The programmer
can then use take or takeExactly and pass the result to toString if they
want to convert part of it to a string.

Now, the one potential issue I see with that is what happens when you try
to print a string with a member that's an infinite range, and it didn't
overload toString, since IIRC, to!string will print out the members in that
case, and you're not directly passing an infinte range. Getting a compiler
error in that situation would definitely be annoying, particularly when
you're trying to do something like print out information on a type that you
do not control.

- Jonathan M Davis





More information about the Digitalmars-d mailing list