Skip to content

Added writer version of toString to Nullable#6198

Merged
dlang-bot merged 1 commit intodlang:masterfrom
JackStouffer:nullable-tostring
Feb 21, 2018
Merged

Added writer version of toString to Nullable#6198
dlang-bot merged 1 commit intodlang:masterfrom
JackStouffer:nullable-tostring

Conversation

@JackStouffer
Copy link
Contributor

Also deprecates the delegate versions because

  1. they're slower most of the time due to the inability to inline the delegate
  2. they were already undocumented
  3. they're rarely directly called by the user, and instead are called by things like to!string, which now automatically call the new overload.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @JackStouffer!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

@JackStouffer JackStouffer force-pushed the nullable-tostring branch 2 times, most recently from 40de20c to bc3f647 Compare February 19, 2018 16:04
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace.

@JackStouffer
Copy link
Contributor Author

@wilzbach Looks like some sort of c library failure with dscanner.

@JackStouffer
Copy link
Contributor Author

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()()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toString of _value can be anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Member

@MetaLang MetaLang Feb 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JackStouffer
Copy link
Contributor Author

@MetaLang Fixed

@dlang-bot dlang-bot merged commit d54386b into dlang:master Feb 21, 2018
@quickfur
Copy link
Member

This PR seems to have introduced a deprecation warning:

void main() {
        import std.typecons : Nullable;
        Nullable!int x;
}

Compile with dmd -unittest test.d:

/usr/src/d/phobos/std/format.d(3723): Deprecation: function `std.typecons.Nullable!int.Nullable.toString!().toString` is deprecated - To be removed after 2.086. Please use the output range overload instead.

Not sure where exactly the problem is, but basically it causes the deprecation warning to appear whenever user code uses Nullable and compiles with -unittest. Would be nice to fix this.

@quickfur
Copy link
Member

@JackStouffer

@JackStouffer
Copy link
Contributor Author

If it's in a unittest, it's a simple manner of marking the block as deprecated unittest, but that line number makes no sense. It's in a unittest block inside of std.format.

@JackStouffer JackStouffer deleted the nullable-tostring branch March 13, 2018 19:35
@quickfur
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants