Two standard libraries?
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sat Jul 14 11:18:21 PDT 2007
"Jason House" <jason.james.house at gmail.com> wrote in message
news:f7atnl$2lm6$1 at digitalmars.com...
>
> Out of curiosity, why is {} used instead of %s?
It makes it easier to make more flexible formatting, and also so you can put
indices into the braces. Indexing the arguments is important because word
order in different languages can be different. For example, say you had the
message in English:
"Employee {} is not in personnel database {}", personName, databaseName
Now look at a language like Japanese, where you'd probably put the database
name first; it'd be arranged something like "in personnel database {},
employee {} does not exist". Now you have to change the order of the
arguments after the format string. But with indexing, you can say {0}
always is the employee name, and {1} always is the database name, so that
you can format them in the correct order for other languages, without having
to change the actual format code. Just change the format string.
Another reason is custom formatting. You know how you can do %x with
printf-style to get a hex number. You can add in some digits to change the
number of digits to display etc. like %08x. But what if you had a class
which could be formatted beyond the simple "use toString (or toUtf8 in Tango
:P)"? Like, what if that class represented a set of coordinates, like
latitude/longitude? There might be more than one way to display it;
including or ommitting some of the pieces of the coordinates, rouding pieces
off to certain precisions etc. What you can do with the {} formatting
specifiers is pass an entirely custom format string after the colon which is
used to format certain types. You'd have to write a custom formatter class
to do this, but the basic formatting framework is already in place; all you
have to do is write a formatting method which parses out that string and
formats the components correctly. No way would you be able to do that with
the % formatters without some ugly additions.
And lastly I think the {} looks prettier ;)
More information about the Digitalmars-d
mailing list