On Saturday, 20 May 2017 at 02:05:21 UTC, Jonathan M Davis wrote:
What we would probably need would be to change msg is a function which generates a message so that derived classes can override that rather than passing a message string.

Further to Moritz's reply showing the existing toString overload taking a delegate. This delegate is not @nogc. Otherwise I was thinking of doing something like this:

//FIXME: uniqueToString should return @nogc UniquePtr!(const char[])
import std.conv;
alias uniqueToString = to!(const char[]);

class MessageEx(E, sinkArgs...) : E
{
    this(A...)(A args)
    {
        super(args);
    }

    //FIXME: delegate not @nogc
/*@nogc*/ override void toString(scope void delegate(in char[]) sink) const
    {
        foreach (a; sinkArgs)
            sink(uniqueToString(a));
    }
}

unittest
{
    auto x = 7;
    throw new MessageEx!(Exception, "x = ", x)(null);
}

The result of uniqueToString would free any memory allocated after the call to sink.

Reply via email to