Poll on improved format strings.

Don Clugston dac at nospam.com.au
Wed Mar 7 01:56:06 PST 2007


Daniel Keep wrote:
> 
> renoX wrote:
>> Hello,
>>
>> I've made a templated format string templates joined in attachment (this new version is improved thanks to Frits van Bommel), but I'm not sure about the syntax of the format string.
>>
>> The idea is: printf format string are interesting because they are powerful but they suck because the %d,%s, etc are in one part of the function and the corresponding variable are in a different part of the function (Tango has the same problem), writef improve this by allowing "... %d",var," ... %s",var2 but it's still not ideal because in the gluing of the various strings, it's easy to forget a space or a comma thus providing a not very good output.
>>
>> So my idea would be to have embedded expression like this "... %08d{var1+var2} ...", but it's not easy to provide a good syntax/semantic, so I'd like some remarks:
>>
>> -Should the mix of printf format and new style format string be allowed? (It is in the current implementation).
>> This has the advantage of nearly keeping  the compatibility, the problem is with the format string "..%d{..." in printf this means a number followed by '{' but with the new format this creates an error.
>> It is possible to escape the '{' to allow this, ie to say that '%{' is '{' so now '%d{' would need to be '%d%{', this has the inconvenient that it's not possible to have the embedding format '%{var}' which would be the shortest syntax..
>> Another possibility would be to say that if you want to have '... %d{ ...' one need to write it has '.... %d',var,'{ ....', this would permit to have the '..%{var}...' embedding syntax.
>>
>> -What to do with non-const char[]?
>> They cannot be parsed by the template, so one possibility is to allow only const char[] parameter or to allow non-const char[] and leave them alone (they may contain printf-style format string). This is what the current implementation is doing but I'm not sure if the added flexibility is not confusing: const char[] can contain both printf-like format and 'new embedded format' but non const char[] can only contain printf-like format string.
>>
>> - Another possibility would be to use a different character '#' (like in Ruby) for these new format string..
>>
>> I'd like some inputs to see if there is a majority in favour of one style or the other..
>>
>> renoX
> 
> To be honest, I think the type suffix needs to go.  After all, if you
> know what the type is at compile-time, why do I need to repeat it?

> Of course, doing that leaves you with the problem of how to specify
> formatting options... but then in the majority of cases, you probably
> don't care; you just want the thing output.

When you use floating point, you want to specify the formatting options 
almost every time -- do you want %f, %e, %g, or %a? And it's almost 
always necessary to specify the number of decimal places to use.
I display integers in hex pretty often, too.

Still, being able to leave all the formatting options out, and write:
"next=%{i+1}" is very appealing.



More information about the Digitalmars-d mailing list