[Issue 4458] Static typing for format strings, when possible

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 28 05:48:09 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=4458


Denis Derman <denis.spir at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |denis.spir at gmail.com


--- Comment #3 from Denis Derman <denis.spir at gmail.com> 2011-02-28 05:45:19 PST ---
(In reply to comment #0)
> In some situations the format string of writef/writefln is not known at
> compile-time, but it most situations it is. So in the frequent cases where the
> format string is known at compile-time I'd like an error at compile-time if the
> type in the format string and the type of the arguments don't match. 
> 
> An error at compile-time is better, it gives the same advantages of static
> typing, and it allows the programmer to catch format string bugs before
> runtime, in all the program, even in parts of the code that aren't run yet (a
> runtime bugs happens only with a specific writefln comes into the thread of
> code being run).
> 
> Currently (dmd v2.047) this compiles with no errors:
> 
> 
> import std.stdio: writefln;
> void main() {
>     float f = 10.5;
>     writefln("%d", f);
> }
> 
> 
> But I'd like an error similar to:
> 
> test.d(4): Error: writefln format string type error, used format '%d' but
> argument 'f' is of type float
> 
> Once written, this new testing routine can be useful for other functions too,
> for the format(), some I/O functions, and for C functions like printf() too
> that sometimes are present in D programs.

Most commonly, format string static checking would catch arg count mismatch at
compile-time:

    writefln("%s --> %s", a);

compiles and builds happily; and launches at runtime:
    std.format.FormatError: std.format Orphan format specifier: %%s --> %s
(Weird error message, in fact.)

Denis

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list