www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How should to!string handle infinite ranges?

reply Paul Backus <snarwin gmail.com> writes:
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
Mar 04
next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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.
Mar 04
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus 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
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
Mar 04
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
On Tuesday, 4 March 2025 at 10:38:24 UTC, Paul Backus 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
`to` needs to be split up `prettyprint` should truncate *everything* to 120 chars
Mar 04
parent Dave P. <dave287091 gmail.com> writes:
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][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
`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.
Mar 04
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Mar 04
prev sibling next sibling parent Dukc <ajieskola gmail.com> writes:
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.
Mar 05
prev sibling parent Inkrementator <invalid invalid.org> writes:
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.
Mar 06