More bugs found in OS code

bearophile bearophileHUGS at lycos.com
Sat Nov 5 06:41:38 PDT 2011


Adam D. Ruppe:

> I do a lot. The way I do it is the arguments are made
> available to the format, but it doesn't always need them
> at runtime.
> 
> string f = showNames ? "%1$s\t%2$d" : "%2$d";
> writefln(f, name, number);
> 
> Though I don't literally use writefln for most
> my code the same idea applies.

Python is strict here:


>>> "%d" % (1)
'1'
>>> "%d" % (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting


Even if you use that idiom, it has costs, measured in bugs. My experience tells me that a sloppy semantics, introduced or kept for a little convenience, always manages to find a way to bite your ass later. So I'd like more tidyness here.

You are allowed to write:

if (showNames)
    writeln("%1$s\t%2$d", name, number);
else
    writeln("%2$d", name);


There are also other solutions that don't compromise the already low safety of writeln, that is a dynamically typed isle in a statically typed language.

Bye,
bearophile


More information about the Digitalmars-d mailing list