Preliminary submission - std.rational and std.typelist
Jakob Ovrum
jakobovrum at gmail.com
Sun Oct 7 02:37:30 PDT 2012
On Sunday, 7 October 2012 at 09:27:54 UTC, Dmitry Olshansky wrote:
> Ehem.. I've been pushing for DIP9 a lot of time. But then I
> find out that it is already here (and been for some time).
>
> Like I said use a special overload of toString that is exactly
> writeTo.
>
> Just define method with this signature:
> void toString(scope void delegate(const(char)[]) sink)
>
> And bingo! It's used in all of formatting stuff like
> write(f)(ln), format, formattedWrite etc.
>
> e.g. before posting I played with this:
>
> import std.stdio, std.format;
>
> struct A{
> int k;
> void toString(scope void delegate(const(char)[]) sink)
> {
> formattedWrite(sink, "[%d]", k);
> }
> }
>
> void main(){
> A a = A(90);
> writeln(a);
> }
Nice! I didn't know that. I thought this toString signature was
only used by BigInt, and not taken advantage of by other routines.
To implement it fully we would still want to change toString for
classes, and probably do something like providing a UFCS function
in object.toString or some other relevant location for
convenience and backwards compatibility.
More information about the Digitalmars-d
mailing list