October 01, 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?
October 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:
>> 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?

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.
October 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:
>> 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?

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.
October 01, 2013
On Tuesday, 1 October 2013 at 07:30:06 UTC, Jacob Carlborg wrote:
> 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.

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 :-)
October 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:
>> 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.
>
> 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 :-)

%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.
October 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
October 02, 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'
>      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?

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...);


1 2
Next ›   Last »