Thread overview
How should to!string handle infinite ranges?
Mar 04
monkyyy
Mar 04
Dave P.
Mar 05
Dukc
March 04

We're currently having a discussion about this on Github. 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?

March 04

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?

I prefer the latter with the adjustment

Can't convert an infinite range to a string, use take to make the range finite

or something like that.

The issue here is that a string is a static non-interactable object.

If we had a way to convert ranges to interactable GUI objects things the situation might be different.

March 04

On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus wrote:

>

We're currently having a discussion about this on Github. 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?

I'd expect a compiler failure over an assumption as to what the user wants.

That being said, I think it could be an option for to!string on a range to truncate after a specific number of items.

I'll put a note in the PR.

-Steve

March 04

On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus wrote:

>

We're currently having a discussion about this on Github. 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?

to needs to be split up

prettyprint should truncate everything to 120 chars

March 04
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



March 04

On Tuesday, 4 March 2025 at 18:13:14 UTC, monkyyy wrote:

>

On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus wrote:

>

We're currently having a discussion about this on Github. 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?

to needs to be split up

prettyprint should truncate everything to 120 chars

This is actually the correct answer. Converting something to a string can’t be done without knowing the intended destination. I’m guessing the most common use of to!string is just as a side effect of some debug prints and so compilation error is annoying. You also want the terminal output to be reasonable.

March 05

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?

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.

March 06

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?

Less surprising: Erroring during compilation.

More Useful: Definitely truncating. It will not matter much for 90% of cases but when it does it will be a huge pain to remove infinite ranges. I also don't see how it could hurt.