Should writef try to statically check for format specifiers?

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 12 08:34:52 PDT 2011


On Mon, 11 Apr 2011 16:35:39 -0400, Andrej Mitrovic <none at none.none> wrote:

> There is a bug here:
>
> import std.stdio;
>
> void main()
> {
>     int index;
>     writef("The index is", index);
> }
>
> Actually I found this bug in some example code:  
> http://d.puremagic.com/issues/show_bug.cgi?id=5836
>
> writef is missing a format specifier. But it still accepts this code.
>
> Is it possible for writef to statically check whether there's a format  
> specifier, assuming there's multiple arguments and the first argument is  
> a string literal?
>
> I realize runtime checks would be out of the question, but I'm looking  
> for compile-time checks when it's possible to do so.

Why would runtime checks be out of the question?  You are already parsing  
the string at runtime, why can't it say "hey, I processed the whole format  
string, but I have these arguments left over"?  Would be a simple if  
statement...

A compile-time check would be nice, but you'd have to pass it as a  
compile-time argument (i.e. a template parameter), which would be  
not-so-nice.

What would be nice is if the compiler could check when you give it a  
string literal, and resort to runtime checks when it was a variable.   I  
don't think that's possible, however.

-Steve


More information about the Digitalmars-d-learn mailing list