Jump to page: 1 2
Thread overview
'pp' for D?
Sep 29, 2013
linkrope
Sep 29, 2013
monarch_dodra
Sep 29, 2013
linkrope
Sep 30, 2013
Ali Çehreli
Sep 30, 2013
monarch_dodra
Sep 30, 2013
linkrope
Oct 01, 2013
John Colvin
Oct 01, 2013
monarch_dodra
Oct 01, 2013
qznc
Sep 30, 2013
bearophile
Oct 01, 2013
Jacob Carlborg
Oct 01, 2013
bearophile
Oct 01, 2013
linkrope
Oct 01, 2013
monarch_dodra
Oct 01, 2013
bearophile
Oct 01, 2013
Daniel Davidson
Oct 02, 2013
Nick Sabalausky
September 29, 2013
I want to pretty-print the representation of a value of a generic type T.
In Ruby, I would use 'pp':

    value = 'hello'
    pp value  # prints "hello" - with quotes!
    value = 42
    pp value  # prints 42

Now, value.to!string eliminates the quotes, should value be of type string.
As a workaround, I put the value into an array to make use of the "undocumented" function formatElement:

    "%(%s%)".format([value])

Ugly! Where does Phobos hide the function I'm looking for?
September 29, 2013
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, '"')?

> Where does Phobos hide the function I'm looking for?

I don't have a solution that's "convenient" enough to satisfy you.
September 29, 2013
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'?
September 30, 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

September 30, 2013
On Monday, 30 September 2013 at 04:20:32 UTC, Ali Çehreli wrote:
> 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:
>
> [SNIP]
>     static if (isSomeString!T) {

This should also take into acount enums, as they get their own printing.

It should either use std.conv's private "isExactSomeString", or use:
static if (isSomeString!T && !is(T == enum)

September 30, 2013
On Monday, 30 September 2013 at 04:20:32 UTC, Ali Çehreli wrote:
> 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

OK.

But putting quotes around a string value is obviously not enough.
What if the string contains a quote? "hell\"o" would become `"hell"o"`!

Seems, I have the choice between:

string repr(T)(T value)
{
    auto writer = appender!string();
    auto fmt = FormatSpec!char("%s");

    formatElement(writer, value, fmt);
    return writer.data;
}

(relying on the "undocumented" formatElement),
or the aforementioned array detour:

string repr(T)(T value)
{
    return "%(%s%)".format([value]);
}
September 30, 2013
linkrope:

> Where does Phobos hide the function I'm looking for?

Surely Phobos should add a prettyPrinting() function, like the function of Python standard library.

Bye,
bearophile
October 01, 2013
On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote:
> I want to pretty-print the representation of a value of a generic type T.
> In Ruby, I would use 'pp':
>
>     value = 'hello'
>     pp value  # prints "hello" - with quotes!
>     value = 42
>     pp value  # prints 42
>
> Now, value.to!string eliminates the quotes, should value be of type string.
> As a workaround, I put the value into an array to make use of the "undocumented" function formatElement:
>
>     "%(%s%)".format([value])
>
> Ugly! Where does Phobos hide the function I'm looking for?

I have one at: https://github.com/patefacio/d-help/blob/master/d-help/pprint/pp.d

The following code outputs the text below:

    import std.stdio;
    import pprint.pp;

    enum Color {
      Red,
      White,
      Blue
    }

    struct R {
      int x = 22;
      string s = "foobar";
    }

    struct S {
      int i;
      R r;
    }

    struct T {
      int []i;
      string []j;
    }


    void main() {
      auto s = S(3);
      auto t = T([1,2,3], ["a", "b", "c"]);

      writeln(pp(Color.Red));
      writeln(pp(42));
      writeln(pp("hello"));
      writeln(pp(s));
      writeln(pp(t));
    }


Outputs

    Red
    42
    "hello"
    {
     (S).i = 3
     (S).r = {
      (R).x = 22
      (R).s = "foobar"
     }
    }
    {
     (T).i = [
      [0]->1
      [1]->2
      [2]->3
     ]
     (T).j = [
      [0]->"a"
      [1]->"b"
      [2]->"c"
     ]
    }

October 01, 2013
On 2013-09-30 23:56, bearophile wrote:

> Surely Phobos should add a prettyPrinting() function, like the function
> of Python standard library.

I would rather have function that generates a pretty representation of a given value. Then it either can be used to print the representation or something else.

-- 
/Jacob Carlborg
October 01, 2013
Jacob Carlborg:

> I would rather have function that generates a pretty representation of a given value. Then it either can be used to print the representation or something else.

That's the point of the Python pprint() (pretty print) function :-)

Bye,
bearophile
« First   ‹ Prev
1 2