dmd 2.029 release
Denis Koroskin
2korden at gmail.com
Thu Apr 23 10:25:55 PDT 2009
On Thu, 23 Apr 2009 18:28:53 +0400, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> Denis Koroskin wrote:
>> Sink is okay, but most my usages belong to one of the two scenarios: 1)
>> I need a string representation of an Object - how is Sink useful
>> here? I just want to call obj.toString() and get the result 2) I need
>> to print it to stdout, thus I call writeln/Stdout(obj); - Sink is of
>> no use here again.
>
> I hear you, but there are quite a few more considerations that apply.
>
> For one, making string the common format of all objects is pretty
> hamfisted. We need to integrate things like binary streams too. Second,
> using string append and composing with it is bound to be inefficient,
> and for multiple reasons. You can't just say that a reasonable way to
> print a matrix is to call toString against it and print the resulting
> string. With the temporary buffer passed-in or not, that's just a
> terrible design.
>
> But you need not fear. Converting to string will remain easy and simple,
> it will only be a particular case of streaming objects out.
>
>
> Andrei
I'm fine with that, especially if entire Phobos will be consistent with it (e.g. std.string.format accepts sink object etc). There a few problems with it, though.
I like the "toStream" name but I believe it is a bit misleading - if toString() returns a string, then toStream() should return stream, too, not accept it.
But what about custom formatting? Previously I stated that current scheme doesn't allow them, but it turned out to be possible.
For example, you introduced custom array formatting:
%(s; ) -> array.toString("s; ", ...);
Similar approach may be used for other user-defined formatting options:
%s -> obj.toString("s");
%d -> obj.toString("d");
...
%(any_set_of_characters) -> obj.toString("any_set_of_characters"); // including inner %() etc
Example: given an array of arrays of ints, print them as follows: "[ [1, 2, 3, 4, ]; [5, 6, 7, 8, ]; [9, a, b, c, ]; ]"
int[][] arrayOfArraysOfInts;
writefln("[ %([%(x, )]; )]", arrayOfArraysOfInts);
Just to make it more clear:
[ %([%(x, )]; )] -> "[ " ~ arrayOfArraysOfInts.toString("[%(x, )]; ") ~ "]"
More information about the Digitalmars-d-announce
mailing list