forked from dlang/phobos
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dlang#5288 from ntrel/ct-format
Issue 13568: Add CT-checked format string overloads to std.format merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
104 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
`std.format.formattedWrite` now accepts a compile-time checked format string | ||
|
||
$(REF formattedWrite, std, format) and related functions now have overloads to | ||
take the format string as a compile-time argument. This allows the format | ||
specifiers to be matched against the argument types passed. Any mismatch or | ||
orphaned specifiers/arguments will cause a compile-time error: | ||
|
||
------- | ||
import std.format; | ||
|
||
void main() { | ||
auto s = format!"%s is %s"("Pi", 3.14); | ||
assert(s == "Pi is 3.14"); | ||
|
||
static assert(!__traits(compiles, {s = format!"%l"();})); // missing arg | ||
static assert(!__traits(compiles, {s = format!""(404);})); // surplus arg | ||
static assert(!__traits(compiles, {s = format!"%d"(4.03);})); // incompatible arg | ||
} | ||
------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters