PHP-style (embedded variables) print statements

Don Clugston dac at nospam.com.au
Thu May 31 08:17:50 PDT 2007


Lars Ivar Igesund wrote:
> Don Clugston wrote:
> 
>> Jarrett Billingsley wrote:
>>> "Don Clugston" <dac at nospam.com.au> wrote in message
>>> news:f3lt8d$2je5$1 at digitalmars.com...
>>>> Note that unlike a tuple solution, it does not cause any template bloat.
>>>> Also doesn't have some of writefln's pitfalls (the case where a string
>>>> contains a % character).
>>> You can't print out the string using %s?
>> The problem is that it's so easy to forget the %s, and have a latent bug
>> that lies undetected.
>>
>>>> Opinions? Is something like this worth considering as an alternative to
>>>> writefln, and to Tango's C# formatting and whisper syntax?
>>> Not to burst your bubble, but I've just never found variable
>>> interpolation to be all that useful, mostly because it's not really
>>> obvious (unless you have some kind of fancy code highlighter) when
>>> something's being interpolated, and because there's already syntaxes that
>>> look fine to me. Some people like it though.
>> Yes, these are the main issues. I know that people have requested it in
>> the past (and it was also touted as something Nemerle can do which D can't
>> <g>). Personally, I thought it was one of very few interesting features of
>> PHP.
>>
>> There's probably a syntax which would be a bit more obvious, but still...
>> BTW, do you feel the same way about the C# syntax? Is it obvious enough?
>>
>> mixin interpolation:
>>    mixin(dprint("current=$i next=$(i+1) const=$(k*3) $str"));
>>
>> C# (available in Tango):
>>    print("current={0} next={1} const={2} {3}", i, i+1, k+3, str));
> 
> To compare, you should use
> 
>      Stdout("current=")(i)(" next=")(i+1)(" const=")(k*3)(" ")(str);
> 
> Since I'm not too happy about $vars , I would find it more interesting if it
> was
> 
>    mixin(dprint("current={i} next={i+1,5} const={k*3:C} {str}));
> 
> which would allow for the specifiers in the C# format, like padding (,5) or
> print as currency (:C).

It's all possible. And that mountain of localisation stuff would not be linked 
in, if it was never used in any format strings.  The :C syntax might be 
confusing, though, because it looks like the ?: operator.
"const={b?c:C:C}"

  Even more interesting would be to use Sprint to
> make it usable in logging.

Should be trivial.
> 
> Would have to get rid of the mixin, though :)



More information about the Digitalmars-d mailing list