Added writer version of toString to Nullable#6198
Conversation
|
Thanks for your pull request, @JackStouffer! 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. |
40de20c to
bc3f647
Compare
std/typecons.d
Outdated
| import core.stdc.stdint : uintptr_t; | ||
| import std.format : singleSpec, FormatSpec, formatValue; | ||
| import std.meta; // : AliasSeq, allSatisfy; | ||
| import std.range.primitives : isOutputRange, put; |
There was a problem hiding this comment.
AFAICT you only need isOutputRange as a module-level import?
| formatValue(writer, _value, fmt); | ||
| } | ||
|
|
||
| deprecated("To be removed after 2.086. Please use the output range overload instead.") |
There was a problem hiding this comment.
Needs the @@@DEPRECATED_2.086@@@ comment, s.t. it can be found.
std/typecons.d
Outdated
| * result is equivalent to `to!string(value)`. | ||
| * | ||
| * Params: | ||
| * writer = A `char` accepting |
bc3f647 to
491f4f4
Compare
|
@wilzbach Looks like some sort of c library failure with dscanner. |
491f4f4 to
cc668a2
Compare
|
Also added overload which gives a string because it can be useful for simple code. |
std/typecons.d
Outdated
| * Returns: | ||
| * A `string` if `writer` and `fmt` are not set; `void` otherwise. | ||
| */ | ||
| string toString()() |
There was a problem hiding this comment.
This can at least be made pure and @safe. Is there a reason you made this overload a template instead of a regular function and marking it with those attributes? Inference doesn't really buy us anything in this case because there are no template arguments that the attributes could depend on.
There was a problem hiding this comment.
The toString of _value can be anything.
There was a problem hiding this comment.
@MetaLang It can depend on template parameters on the containing struct.
It's actually a bad idea to put explicit attributes on templates, and members of templated aggregates, because that constrains what types the template can be instantiated with. It should be rather left up to the compiler to infer them, and unittests to enforce mandatory attributes for specific instantiations of the template.
There was a problem hiding this comment.
Fair enough, but I don't think making it a no-arg template is necessary as it's inside a templated struct.
import std.array : appender;
import std.format;
import std.range;
struct Test()
{
string toString()
{
auto app = appender!string();
auto spec = singleSpec("%s");
toString(app, spec);
return app.data;
}
/// ditto
void toString(W)(ref W writer, const ref FormatSpec!char fmt)
if (isOutputRange!(W, char))
{
import std.range.primitives : put;
put(writer, "Nullable.null");
}
}
void main()
{
import std.stdio;
import std.traits;
writeln(hasFunctionAttributes!(Test!().toString, "@safe", "pure")); //Prints "true"
writeln(!hasFunctionAttributes!(Test!().toString, "nothrow", "@nogc", "@system")); //Prints "false"
}
As you can see, the inference is done even though toString is not an explicitly templated function. Put in a call to an @system function and this will print "false" and "true" instead.
cc668a2 to
8c67a25
Compare
|
@MetaLang Fixed |
|
This PR seems to have introduced a deprecation warning: Compile with Not sure where exactly the problem is, but basically it causes the deprecation warning to appear whenever user code uses |
|
If it's in a unittest, it's a simple manner of marking the block as |
|
I know it makes no sense, and I don't have time to dig into it right now, that's why I'm asking. :-D Otherwise I'd fix it myself. |
Also deprecates the delegate versions because
to!string, which now automatically call the new overload.