www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12106] New: Formatting syntax for a range of typecons tuples

https://d.puremagic.com/issues/show_bug.cgi?id=12106

           Summary: Formatting syntax for a range of typecons tuples
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 08 2014