On Thursday, 11 January 2018 at 22:07:42 UTC, H. S. Teoh wrote:
On Thu, Jan 11, 2018 at 04:38:57PM -0500, Steven Schveighoffer via Digitalmars-d-announce wrote:
On 1/11/18 4:12 PM, kdevel wrote:
> On Thursday, 11 January 2018 at 20:40:01 UTC, Dmitry > Olshansky wrote:
> > What did you expect?
> > To be honest: A compile time error. Modern C compilers can > check such format strings. Example: GCC 6:

But dmd isn't a C compiler, nor does it have to worry about the problems C has (namely, untyped varargs). To dmd, printf is just another function, there's nothing special about it.

Yeah, checking C-style printf formats isn't dmd's problem.

The Phobos equivalent of printf, however, *does* support compile-time format checking in the latest version:

        writefln!"%s %d %d"("abc", 1); // "Orphan format specifier: %d"
writefln!"%s %d"("abc", 1, 2); // "Orphan format arguments: args[2..3]" writefln!"%s %d"(1, "abc"); // "Incorrect format specifier for range: %d" writefln!"%f"(1); // "incompatible format character for integral argument: %f"

Best of all, this is all done via CTFE in the library code, no hard-coding in the compiler necessary. You can implement your own compile-time checker for your own DSLs in the same way, without needing to hack the compiler.

Interesting, guess this was added last April with dmd 2.074?


You or someone should write up a blog post about this, with both this example and expanding on how D enables this kind of functionality in general.

Reply via email to