A char accepting delegate
import std.format : format; Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ]; // Default format assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`); // One Format for each individual component assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`); assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`); // One Format for all components assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`); // Array of Tuples assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`);
import std.exception : assertThrown; import std.format : format, FormatException; // Error: %( %) missing. assertThrown!FormatException( format("%d, %f", tuple(1, 2.0)) == `1, 2.0` ); // Error: %( %| %) missing. assertThrown!FormatException( format("%d", tuple(1, 2)) == `1, 2` ); // Error: %d inadequate for double assertThrown!FormatException( format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0` );
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.)))