View source code
Display the source code in std/typecons.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.typecons.Tuple.toString - multiple declarations

Function Tuple.toString

Converts to string.

string toString() const;

Returns

The string representation of this Tuple.

Function Tuple.toString

Formats Tuple with either %s, %(inner%) or %(inner%|sep%).

void toString(DG) (
  scope DG sink
) const;

void toString(DG, Char) (
  scope DG sink,
  scope const ref FormatSpec!Char fmt
) const;

Formats supported by Tuple
FormatDescription

%s

Format like Tuple!(types)(elements formatted with %s each).

%(inner%)

The format inner is applied the expanded Tuple

so it may contain as many formats as the Tuple has fields.

%(inner%|sep%)

The format inner is one format

that is applied on all fields of the Tuple. The inner format must be compatible to all of them.

Parameters

NameDescription
sink A char accepting delegate
fmt A std.format.FormatSpec

Example

import std.format : format;

Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ];

// Default format
writeln(format("%s", tuple("a", 1))); // `Tuple!(string, int)("a", 1)`

// One Format for each individual component
writeln(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10))); // `0x1 v 1.0000 w 0xa`
writeln(format("%#x v %.4f w %#x", tuple(1, 1.0, 10).expand)); // `0x1 v 1.0000 w 0xa`

// One Format for all components
// `>abc< & >1< & >2.3< & >[4, 5]<`
writeln(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])));

// Array of Tuples
writeln(format("%(%(f(%d) = %.1f%);  %)", tupList)); // `f(1) = 1.0;  f(2) = 4.0;  f(3) = 9.0`

Example

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`
);
}

Authors

Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara

License

Boost License 1.0.