Poll on improved format strings.
Don Clugston
dac at nospam.com.au
Wed Mar 7 05:41:07 PST 2007
renoX wrote:
> Daniel Keep Wrote:
>> 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?
>
> If the compatibility with printf(allowing %d without {...} format string) is removed (and I think I'll do this) then %{var} would be also allowed.
>
>> 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.
>>
>> So how about something like this:
>>
>> Expansion ::= "$" ExpansionSpec
>>
>> ExpansionSpec ::= FormattingOptions ExpansionExpr
>> ExpansionSpec ::= ExpansionExpr
>>
>> FormattingOptions ::= "(" string ")"
>>
>> ExpansionExpr ::= "{" D_Expression "}"
>> ExpansionExpr ::= D_Identifier
>>
>> So the example above becomes "... $(08){var1+var2} ...": one character
>> longer, but gives you more freedom as to what you can put in the
>> formatting options. Plus, if you don't care how it's formatted, you can
>> use "... ${var1+var2} ...", and if you just want to print a variable
>> out, you can use "... $somevar ...".
>
> Printf-Format string are quite powerful, and they're well known so I think their syntax should be kept, just with the obligation to follow by {...}: so the following would work:
> %{var}, %08X{var}, etc.. (%d{var} and %s{var} wouldn't be useful anymore, I agree but to avoid surprising C programmers, they should be kept).
>
> $somevar or %somevar without {...}, I don't know, it's shorter, sure but less readable I think.
I agree. You'd also need to worry about "%f{abc}" -- is this a local
variable called 'f', followed by the text "{abc}", or is it printing a
floating point number called abc ?
More information about the Digitalmars-d
mailing list