Bug 21456 – std.format does not accept enum member with string base type as template parameter
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-12-05T07:55:04Z
Last change time
2021-05-10T19:38:45Z
Keywords
pull
Assigned to
No Owner
Creator
ricky maicle
Comments
Comment #0 by rmaicle — 2020-12-05T07:55:04Z
D 2.094.2
Linux 5.9.11-3-MANJARO x86_64
-----------------------------
The std.format overload accepts a compile-time string as format string.
This code compiles:
enum FMTS = "%s is %s";
assert (format!FMTS("Pi", 3.14) == "Pi is 3.14");
This code also compiles:
enum {
FMTS = "%s is %s"
}
assert (format!FMTS("Pi", 3.14) == "Pi is 3.14");
This code does not compile:
enum FMTS : string {
ONE = "%s is %s"
}
assert (format!FMTS("Pi", 3.14) == "Pi is 3.14");
Formatted error message:
Error: template std.format.format cannot deduce function from argument
types !("%s is %s")(string, double), candidates are:
../src/phobos/std/format.d(6514,13): format(alias fmt, Args...)(Args args)
with fmt = ONE,
Args = (string, double)
must satisfy the following constraint:
isSomeString!(typeof(fmt))
The std.format overload accepting compile-time string as format string
should accept named enums with a string base type since the enum member
value can be determined during compile-time.
Comment #1 by dlang-bot — 2021-05-02T17:51:41Z
@berni44 created dlang/phobos pull request #8029 "Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter" fixing this issue:
- Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter
https://github.com/dlang/phobos/pull/8029
Comment #2 by dlang-bot — 2021-05-04T12:36:07Z
dlang/phobos pull request #8029 "Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter" was merged into master:
- 9d71253c561727a7793bc70cdb353795fa448007 by berni44:
Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter
https://github.com/dlang/phobos/pull/8029
Comment #3 by dlang-bot — 2021-05-10T16:30:10Z
dlang/phobos pull request #8057 "Revert "Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter"" was merged into master:
- 4a9a203087ef84df79014682aaa91e814b3224ee by berni44:
Revert "Fix Issue 21456 - std.format does not accept enum member with string base type as template parameter"
This reverts commit 974a88a967e2e4c6495bd469d3237a4f5aa8b4ab.
https://github.com/dlang/phobos/pull/8057
Comment #4 by bugzilla — 2021-05-10T19:38:45Z
As can be read in the discussions of the PRs, this has been seen as invalid, because it causes too much problems to accept such enums as strings, while there is only little use. It was suggested to use FMTS.ONE.representation instead, but that doesn't work, because the representation is immutable(ubyte)[] and not string.
Anyway, a working workaround is
enum FMTS : string
{
ONE = "%s is %s"
}
immutable string fmt = FMTS.ONE;
assert (format!fmt("Pi", 3.14) == "Pi is 3.14");