Improvement of format string. Re: Suggestion for a D project/std module: templated format

renoX renosky at free.fr
Fri Feb 23 10:58:53 PST 2007


Don Clugston a écrit :
> renoX wrote:
>> Miles Wrote:
>>
>>> 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.
>>
>> Yes, %s and the like are useless except when you need a special format 
>> like %08d.
>>
>> In Ruby, they provide an escape syntax 'puts "test #{variable}"' 
>> without format for the default embedding.
>>
>> IMHO in D we could use a similar syntax: writef("text %{variable}"); 
>> and writef("test %08d{variable2}"); the %{} embedded in the strings 
>> helps a lot readability I think.
> 
> 
> IMHO, to be really useful, it would need to be possible to embed 
> expressions in the string (not just variable names). This is 
> tantalizingly close to being possible with string mixins.

Not 'tantalizingly close to being possible': it works!

I've posted the implementation in another thread 'Improvement on format 
strings, take two.'
and indeed: mixin(Putf!("text: %d{x+y+1}")); does what's expected.
Of course format string containing %.{} format must be const char[].

I had to add a limitation to escape '{' at first I planned to do '\{' 
with would allow %{x} to work, but dmd tells me 'invalid escape', so 
'%{' is the escape for '{' and you have to specify %d[x}

The only annoying part is the need to put the mixin keyword, too bad 
there isn't some special syntax: $putf!(.. ) would be acceptable for 
example or even better putf!!(..)
mixin(putf!(..)) looks ugly.

renoX
PS: while I won't do the 'templated format' suggestion, IMHO my 
implementation of putf would be a good start for anyone who wants to do 
it: it has format string matching template code.



More information about the Digitalmars-d mailing list