-
-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #10559 convert infinite range to string #10660
Conversation
Thanks for your pull request and interest in making D better, @eslamsamy226! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems about right but not sure if this is the way we want to fix it. Perhaps needs more comments here.
One potential benefit of the current behavior (compile error) is that it can help catch mistakes. How likely is it that someone trying to convert an infinite range to a string is doing it on purpose, rather than by mistake? |
The issue consists for two parts, compilation error with to!string and infinite loop with format("%s", ...), the fix now covers both by converting them to string as suggested in the issues, But I'm open to any other suggestions to go about it. |
The way I see it there are two possibilities:
I'm not sure which is better. It might be a good idea to try and gather feedback on which option people would find less surprising. |
I'm new to the D community and this actually is my first pull request here, so I'd appreciate if you suggest how to gather feedback, Thanks |
Short of adding the infinity symbol (I do not recommend this), I don't see how this could be substantially improved. Printing data, and with that pretty printing, is not an easy task to get right and there are plenty of opinions readily available. I've enabled CI. |
@eslamsamy226 I've posted a thread on the D forums: https://forum.dlang.org/post/bnwiqwctksnukdylrzxb@forum.dlang.org |
@rikkimax I think the infinity symbol ∞ or any thing that indicates that this is an infinite sequence will be nice to be provided by the standard library, but this my subjective opinion. |
I do not like that you are assuming that the user wants a specific behavior, and that the behavior is not configurable. My recommendation is to add an overload to So to demonstrate my recommendation: import std.range : repeat;
import std.conv : to;
auto infiniteRange = repeat(42);
static assert(!__traits(compiles, to!string(infiniteRange))); // doesn't compile as is
assert(to!string(infiniteRange, truncateAfter: 3) == "[42, 42, 42, ...]");
assert(to!string(infiniteRange, truncateAfter: 3,
truncateText: "and so on") == "[42, 42, 42, and so on]"); For reference, this is similar to CSS text-overflow feature: https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow The difference being the trigger is an element width instead of a number of elements. This also allows nice mechanisms even for non-infinite ranges. |
For I would actually be OK with |
This is poor separation of concerns IMO. If the user wants to truncate the range to a custom length before converting to Likewise for "what to write after"—anyone who wants a string formatted in a specific way should use |
Yeah, actually this is not as easy as I was thinking, because format doesn't make this easy, and doesn't help for types that do not have a length. Instead, a type that does the truncation output would be better. |
In general, the correct thing to do with infinite ranges if a function can't handle them is for the template constraint to reject them, and then the user can use something like Testing this out right now, However,
So, I would say that we don't want the changes in this PR. The caller can just use |
My attempt at starting the type. This doesn't work with formatting individual elements, and it doesn't forward other things like lenth, so it will have to be augmented and further tested. UPDATE: realized we don't need to customize the string type, struct LimitedRange(R)
{
private import std.range;
private enum oneType = is(ElementType!R == string);
static if(oneType)
alias Elem = string;
else
{
private import std.sumtype;
alias Elem = SumType!(ElementType!R, string);
}
private R src;
private long left;
private string msg = "...";
bool empty() => src.empty || left < 0;
Elem front() {
static if(oneType)
static Elem get(Elem val) => val;
else
static Elem get(T)(T val) => Elem(val);
if(left)
return get(src.front);
else
return get(msg);
}
void popFront() {
--left;
if(!src.empty) src.popFront;
}
}
auto limitedRange(R)(R range, size_t elements = 3, string msg = "...")
{
return LimitedRange!(R)(range, elements, msg);
}
void main()
{
auto rng = [1, 2, 3, 4, 5];
import std.stdio;
import std.range;
writeln(rng.limitedRange(4));
writeln(repeat(42).limitedRange(4, "etc."));
writeln(repeat("hi").limitedRange(4, "etc."));
import std.conv;
assert(rng.limitedRange.to!string == "[1, 2, 3, ...]");
} |
It looks like the consensus on the forum is that trying to convert an infinite range to a string with For example:
...where |
On a more general note, it’s a bit unfortunate when the brainstorming/vetting happens only after a patch has already been implemented and a corresponding PR is submitted. Sure, a thousand files on the issue tracker is quite a lot and it would be a more than unrealistic expectation to expect anyone to look into (or remember) all of them. In this particular case, however or at least, the issue was only about 6 months old and also got labelled “Good First Issue”. Either way, I’m not sure whether it leaves a “Good First Impression” when people seemingly only bother reading feature requests after someone already spent time implementing them. This has already bothered me a bit with #10625 that was kinda treated as if it were a novel feature addition rather than the implementation of a feature request from more than 11 years ago – one that, by the way, had only had accumulated positive feedback and no rejection. |
@0xEAB You raise a good point. I think most people follow PRs more closely than issues because the volume of issues is much higher and harder to keep up with. Still, the earlier in the process we can handle things, the better, and we can definitely make more of an effort that we do now. As of today, I've updated my Github settings to notify me of new issues in this repository, and will try to drop in and give feedback if/when appropriate. Hopefully other members of @dlang/team-phobos will do the same. |
I think failing to compile is probably the right thing to do. |
@pbackus Sounds like we should close this PR then? |
Closing this PR since we've decided against this solution to issue #10559. |
No description provided.