Tuples printing
H. S. Teoh via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 3 09:11:20 PDT 2014
On Thu, Jul 03, 2014 at 09:52:35AM +0000, bearophile via Digitalmars-d wrote:
[...]
> void main() {
> import std.stdio, std.typecons;
>
> alias RGB = Tuple!(ubyte,"R", ubyte,"G", ubyte,"B");
> const arr = [RGB(1, 2, 3), RGB(4, 5, 6), RGB(7, 8, 9)];
>
> writeln(arr);
> }
>
>
> It prints:
>
> [const(Tuple!(ubyte, "R", ubyte, "G", ubyte, "B"))(1, 2, 3),
> const(Tuple!(ubyte, "R", ubyte, "G", ubyte, "B"))(4, 5, 6),
> const(Tuple!(ubyte, "R", ubyte, "G", ubyte, "B"))(7, 8, 9)]
>
>
> If your range of tuples grows larger, the printing becomes too much
> long and too much noisy. The signal gets almost lots.
>
> A simple solution is to print tuples inside ranges as just (field1,
> field2, ...), and keep the same printing style if you print a single
> tuple:
>
> writeln(RGB(1, 2, 3))
> writeln(arr);
> ==>
> Tuple!(ubyte, "R", ubyte, "G", ubyte, "B"))(1, 2, 3)
> [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
[...]
I like this idea.
Looking at the code, it seems that Tuple.toString was specifically
written to output tuples in the current format, though admittedly it's
rather verbose. We *could* probably overload/rewrite it so that it
permits %(...%) format specifiers, upon which the user could specify a
different format, like the ones you suggested.
(This could constitute a legitimate (rather than toy) use case for
custom format specifiers, that I've written about. :-P)
T
--
MACINTOSH: Most Applications Crash, If Not, The Operating System Hangs
More information about the Digitalmars-d
mailing list