Bug 12106 – Formatting syntax for a range of typecons tuples
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-08T03:41:00Z
Last change time
2016-01-07T21:13:40Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-02-08T03:41:06Z
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.