1 import std.format : format;
2
3 Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ];
4
5 // Default format
6 assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`);
7
8 // One Format for each individual component
9 assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`);
10 assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`);
11
12 // One Format for all components
13 assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`);
14
15 // Array of Tuples
16 assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`);
1 import std.exception : assertThrown;
2 import std.format : format, FormatException;
3
4 // Error: %( %) missing.
5 assertThrown!FormatException(
6 format("%d, %f", tuple(1, 2.0)) == `1, 2.0`
7 );
8
9 // Error: %( %| %) missing.
10 assertThrown!FormatException(
11 format("%d", tuple(1, 2)) == `1, 2`
12 );
13
14 // Error: %d inadequate for double
15 assertThrown!FormatException(
16 format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0`
17 );
Formats Tuple with either %s, %(inner%) or %(inner%|sep%).
$(TABLE2 Formats supported by Tuple, $(THEAD Format, Description) $(TROW $(P `%s`), $(P Format like `Tuple!(types)(elements formatted with %s each)`.)) $(TROW $(P `%(inner%)`), $(P The format `inner` is applied the expanded `Tuple`$(COMMA) so it may contain as many formats as the `Tuple` has fields.)) $(TROW $(P `%(inner%|sep%)`), $(P The format `inner` is one format$(COMMA) that is applied on all fields of the `Tuple`. The inner format must be compatible to all of them.)))