Improvement of format string. Re: Suggestion for a D project/std module: templated format
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Fri Feb 23 16:49:14 PST 2007
renoX wrote:
> 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.
That is on the radar. Ideally, if something expands to an expression, it
should be implementable with the function call syntax.
Andrei
More information about the Digitalmars-d
mailing list