Suggestion for a D project/std module: templated format

Miles _______ at _______.____
Tue Feb 20 22:07:47 PST 2007


Derek Parnell wrote:
> For what its worth, I tend to just use '%s' in writefln() calls, and almost
> never use any of the other format codes, regardless of the data type being
> supplied in the arguments.

This means that you have a little more run-time code being executed to
type-id each argument and call the appropriate toString(). I.e.:

>     char[] theName;
>     int theAge;
> 
>     std.stdio.writefln("Hi, my name is %s and I'm %s years old",
>                           theName, theAge);

In this case, format will have to type-identify both arguments at
run-time every time this statement is executed, and the results will
always be char[] for the second argument and int for the third, it won't
change. This is a little bit of useless computation. Performance impact
is small, but it exists, and it bothers me (not the type-id solely, but
the format string parsing).

>     std.stdio.writefln("Hi, my name is %s and I'm %s years old",
>                           toString(theName), toString(theAge) );

What about is you want leading zeros and a forced '+' sign on the
integer number? Or want to format a float number as Sci notation with 2
decimal places, padded into a column 12-spaces wide...

> I'm not saying your idea is bad, just that its not going to be universally
> wanted.

Yeah, sure. It is just how I see that format should have worked since
the old C days, with compile-time type checking, argument counting, and
no useless computation (neither type-identifying, nor string parsing) at
run-time. Now that D have some powerful mixins, added to templates, this
makes the whole sense.



More information about the Digitalmars-d mailing list