On 05/01/2018 01:44 PM, Per Nordlöw wrote:
In which cases (if any) is it possible to make a delegate-style
implementation of toString such as
void toString(scope void delegate(const(char)[]) sink) const
@trusted pure
{
// sink("...");
// sink.formattedWrite!"..."(...);
}
pure?
You have to mark `sink` as pure, too:
void toString(scope void delegate (const(char)[]) pure sink)
const @trusted pure
Then the toString method itself works, but it may not be callable by
other code that wants to use an impure sink.
For example, `format` works, but `writeln` doesn't:
----
struct S
{
void toString(scope void delegate(const(char)[]) pure sink)
const @trusted pure
{
import std.format: formattedWrite;
sink("...");
sink.formattedWrite!"%s"(" ...");
}
}
void main()
{
import std.format: format;
import std.stdio: writeln;
writeln(format("%s", S())); /* Ok. Prints "... ...". */
writeln(S()); /* Nope. writeln would like to use an impure sink. */
}
----
By the way, you shouldn't mark toString as @trusted when `sink` is @system.