digitalmars.D.learn - 'pp' for D?
- linkrope (13/13) Sep 29 2013 I want to pretty-print the representation of a value of a generic
- monarch_dodra (4/8) Sep 29 2013 That seems excessive. What happened to format(`"%s"`, s) or
- linkrope (8/15) Sep 29 2013 Of course, this works well when I know that the value is of type
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (22/36) Sep 29 2013 I don't know a Phobos function either but the following should work:
- monarch_dodra (6/33) Sep 29 2013 This should also take into acount enums, as they get their own
- linkrope (19/41) Sep 30 2013 OK.
- John Colvin (2/6) Oct 01 2013 Would would you want it be become?
- monarch_dodra (8/14) Oct 01 2013 Don't understand. Did you mean "Would would you want it be
- qznc (4/10) Oct 01 2013 I believe the basic idea of repr and friends is to output
- bearophile (5/6) Sep 30 2013 Surely Phobos should add a prettyPrinting() function, like the
- Jacob Carlborg (6/8) Oct 01 2013 I would rather have function that generates a pretty representation of a...
- bearophile (5/8) Oct 01 2013 That's the point of the Python pprint() (pretty print) function
- linkrope (5/12) Oct 01 2013 How about "%r" in 'format'?
- monarch_dodra (5/20) Oct 01 2013 %r is already taken: It means "raw". It's used as a way to use
- bearophile (4/8) Oct 01 2013 I didn't know that -.-
- Daniel Davidson (55/68) Sep 30 2013 I have one at:
- Nick Sabalausky (11/28) Oct 01 2013 How 'bout either of these?:
I want to pretty-print the representation of a value of a generic type T. In Ruby, I would use 'pp': value = 'hello' value = 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?
Sep 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.
Sep 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: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'?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, '"')?
Sep 29 2013
On 09/29/2013 03:38 PM, linkrope wrote:On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra 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); } AliOn Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote: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'?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, '"')?
Sep 29 2013
On Monday, 30 September 2013 at 04:20:32 UTC, Ali Çehreli wrote:On 09/29/2013 03:38 PM, linkrope wrote: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)On Sunday, 29 September 2013 at 18:14:03 UTC, monarch_dodra wrote:I don't know a Phobos function either but the following should work: [SNIP] static if (isSomeString!T) {On Sunday, 29 September 2013 at 14:31:15 UTC, linkrope wrote: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'?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, '"')?
Sep 29 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); } AliOK. 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]); }
Sep 30 2013
On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:But putting quotes around a string value is obviously not enough. What if the string contains a quote? "hell\"o" would become `"hell"o"`!Would would you want it be become?
Oct 01 2013
On Tuesday, 1 October 2013 at 09:21:44 UTC, John Colvin wrote:On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:Don't understand. Did you mean "Would would you want it be become"? I think he meant that he *wanted* it to become "hell\"o". I said: "why don't you just print `"%s"`, to which he retorted by saying it would choke on his input of `hell"o`, which would print: "hell"o" Which would be wrong.But putting quotes around a string value is obviously not enough. What if the string contains a quote? "hell\"o" would become `"hell"o"`!Would would you want it be become?
Oct 01 2013
On Tuesday, 1 October 2013 at 09:21:44 UTC, John Colvin wrote:On Monday, 30 September 2013 at 21:24:28 UTC, linkrope wrote:I believe the basic idea of repr and friends is to output language literals. You could copy&paste the output back into a source file and use it.But putting quotes around a string value is obviously not enough. What if the string contains a quote? "hell\"o" would become `"hell"o"`!Would would you want it be become?
Oct 01 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
Sep 30 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
Oct 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
Oct 01 2013
On Tuesday, 1 October 2013 at 07:30:06 UTC, Jacob Carlborg wrote:On 2013-09-30 23:56, bearophile wrote:How about "%r" in 'format'? Then for arrays, "%(%s, %)" would in fact be "%(%r, %)" while "%-(%s, %)" would in fact be "%(%s, %)" - breaking existing code; punishable by 'xxformat' for at least one year :-)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.
Oct 01 2013
On Tuesday, 1 October 2013 at 19:47:16 UTC, linkrope wrote:On Tuesday, 1 October 2013 at 07:30:06 UTC, Jacob Carlborg wrote:%r is already taken: It means "raw". It's used as a way to use formatting, even when writing in a binary file. You can even use "%+r" and "%-r" to specify the endian-ness you want to write in. It's fun. Makes writing file headers *real* easy.On 2013-09-30 23:56, bearophile wrote:How about "%r" in 'format'? Then for arrays, "%(%s, %)" would in fact be "%(%r, %)" while "%-(%s, %)" would in fact be "%(%s, %)" - breaking existing code; punishable by 'xxformat' for at least one year :-)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.
Oct 01 2013
monarch_dodra:%r is already taken: It means "raw". It's used as a way to use formatting, even when writing in a binary file. You can even use "%+r" and "%-r" to specify the endian-ness you want to write in. It's fun. Makes writing file headers *real* easy.I didn't know that -.- Bye, bearophile
Oct 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' value = 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" ] }
Sep 30 2013
On Sun, 29 Sep 2013 16:31:14 +0200 "linkrope" <linkrope github.com> wrote:I want to pretty-print the representation of a value of a generic type T. In Ruby, I would use 'pp': value = 'hello' value = 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?How 'bout either of these?: void pp(T)(T value) { writefln("%(%s%)", [value]); } string prettyVal(T)(T value) { return "%(%s%)".format([value]); } pp(...whatever...); string s = prettyVal(...whatever...);
Oct 01 2013