'pp' for D?

Ali Çehreli acehreli at yahoo.com
Sun Sep 29 21:20:31 PDT 2013


On 09/29/2013 03:38 PM, linkrope wrote:
> On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra wrote:
>> On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
>>> As a workaround, I put the value into an array to make use of the
>>> "undocumented" function formatElement:
>>>
>>>    "%(%s%)".format([value])
>>
>> That seems excessive. What happened to format(`"%s"`, s) or
>> format("\"%s\"", s) or text('"', s, '"')?
>
> Of course, this works well when I know that the value is of type string.
>
> But I have a template and I want to print the representation of (T value):
> "hello" (with quotes) or just 42 (without)
>
> Phobos does this for array elements, but it seems that there's nothing
> like 'repr'?

I don't know a Phobos function either but the following should work:

import std.stdio;
import std.traits;

void pp(T)(File output, T value)
{
     static if (isSomeString!T) {
         output.writef(`"%s"`, value);

     } else {
         output.write(value);
     }
}

void pp(T)(T value)
{
     pp(stdout, value);
}

void main()
{
     pp("hello");
     pp(42);
}

Ali



More information about the Digitalmars-d-learn mailing list