D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12106 - Formatting syntax for a range of typecons tuples
Summary: Formatting syntax for a range of typecons tuples
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-08 03:41 UTC by bearophile_hugs
Modified: 2016-01-07 21:13 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-02-08 03:41:06 UTC
This is a syntax to format the key and values of an associative array:

import std.stdio: writefln;
void main() {
    auto aa = [1: 10, 2: 20, 3: 30];
    writefln("%(%d: %d\n%)", aa);
}


Its output:

1: 10
2: 20
3: 30


I'd often like to format in a similar way a range of std.typecons.Tuple, using some syntax:


import std.stdio: writefln;
import std.range: zip;
void main() {
    auto r1 = zip([1, 2, 3], [10, 20, 30]);
    writefln("%(%d: %d\n%)", r1);
    auto r2 = zip([1, 2, 3], [10, 20, 30], [100, 200, 300]);
    writefln("%(%d: %d, %d\n%)", r2);
}


Such ranges of tuples are rather common, they are generated by zip() and group() and other Phobos functions, and in future by the enumerate() function (Issue 5550 ) and AA.byPair (Issue 5466 ) too.

Here I have used a syntax similar to the associative array one. If you use only one formatting % then it formats the whole tuple, otherwise it requires as many % as the fields of the tuple, not one more not one less.

Unfortunately this simple syntax is ambiguous when you have a range of 1-tuples:


import std.stdio: writefln;
import std.range: zip;
void main() {
    auto r0 = zip([10, 20, 30]);
    writefln("%(%s\n%)", r0);
}


Currently this works and outputs:

Tuple!int(10)
Tuple!int(20)
Tuple!int(30)

So this whole idea seems to need a way to disambiguate this case. Ideas are welcome.
Comment 1 Jakob Ovrum 2016-01-07 21:13:40 UTC
Fixed by:

https://github.com/D-Programming-Language/phobos/pull/3833