-
-
Notifications
You must be signed in to change notification settings - Fork 739
Fix Issue 13971 #2875
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 Issue 13971 #2875
Conversation
*/ | ||
mixin template forwardToStringTo(alias symbol) | ||
{ | ||
string toString() { |
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.
Although nullary toString
like this is important to include for classes so it overrides Object.toString
, please add sink-based toString
.
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.
Sorry but I dont't know what you mean by this (I even don't know what sink-based means). Can you please give me some example?
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.
void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt)
{
import std.format : formatValue;
import std.internal.scopebuffer : scopeBuffer;
char[256] buffer; // arbitrary size
auto sBuffer = scopeBuffer(buffer[]);
formatValue(sBuffer, symbol, fmt);
sink(sBuffer[]);
}
It might be prudent to wrap the usage of ScopeBuffer
in @trusted
.
A better implementation might be to iterate this[]
and calling the sink with the stringified result of each element, although this would need to take custom toString
(on the range type) and such into account, so this logic probably belongs somewhere in std.conv or std.format, not anywhere in the std.container package.
See also the wiki page for sink-based toString.
(Note also the brace style; this style is the Phobos convention)
Otherwise LGTM assuming the issue is valid... I'm wary about any change that conflates containers and ranges, as it cannot be taken to its ultimate conclusion. I'm inclined to think that stringification of the container should print its identity (ala |
TL;DR First I agree with jakob about conflating containers and ranges. This will just move the problem (the misunderstanding of how containers and ranges are different) to the next place where a container behaves different from a range. The correct thing to do is to require the user to use opSlice if he needs a range over the container. Second the question is, what should toString be used for and what is the most convenient thing for the developer. If you print something pretty for your users, you'll usually have to write your own function anyway since phobos cannot anticipate all desired formats. This makes toString more of a tool for the developer, to print the contents inside a debugger or during println-debugging. Having identity and type information is useful here, however
Is much to verbose and contains useless information. I think
would be a good compromise, if 20D9590 identifies the payload uniquely, like Jakob suggested. Printing the contents alongside with this is often not what I want, because the container might just be to big for an output of all it's contents to be meaningful. And if I want to print the output as well, it's as hard as:
I am opposed to this change, because printing the output as well is easy with the current API but printing only type and identity becomes impossible with this PR and current API. |
You convinced me that this is not a good solution, maybe we can just improve doc with some example so it would much more clear how to print containers. |
So close this? |
Huh, I don't know why I didn't read this fully before. I have some comments:
This can be fixed, the code is in In general, I think I agree that the |
I think it should print the container's identity.
Neither AA's nor builtin slices abide by the Phobos container concept, they're not even close. I think builtin slices in particular are not a good argument as they are ranges. I stand by my argument from before. We need to improve documentation so the user understands the container/range separation; ad-hoc conflation of the two concepts in Phobos does not solve the underlying problem, it just makes it worse. Containers have historically had really poor documentation, and indeed the concept is not completely fleshed out yet. It's too late to change inbuilt types ( |
Documentation doesn't fix this. 9 times out of 10, a user will want to print a container and find the current output useless. It doesn't make a whole lot of sense to say RTFM at that point. I think printing a container should do the most expected thing, and leave the obtuse thing for a specialized function.
Yes, I agree, we should document that printing a container prints its elements. |
https://issues.dlang.org/show_bug.cgi?id=13971