Thread overview
Should we have prettier string printing for exceptions and std elemts?
Oct 08, 2017
Fra Mecca
Oct 09, 2017
bauss
Oct 09, 2017
jmh530
Oct 09, 2017
jmh530
Oct 10, 2017
bauss
Oct 10, 2017
Seb
October 08, 2017
Hi,
I have noticed that there are elements of core and phobos that are pretty ugly when printed via writeln.

One example is container.Array, but also exceptions.

Should we prettify all of them to have a result similar to the one in python?
October 09, 2017
On Sunday, 8 October 2017 at 21:48:05 UTC, Fra Mecca wrote:
> Hi,
> I have noticed that there are elements of core and phobos that are pretty ugly when printed via writeln.
>
> One example is container.Array, but also exceptions.
>
> Should we prettify all of them to have a result similar to the one in python?

I believe we shouldn't; at least not for the default writeln, writefln etc.

If anything there should be made alternative functions to do that.
October 09, 2017
On Sunday, 8 October 2017 at 21:48:05 UTC, Fra Mecca wrote:
>
> One example is container.Array
>

Ideally you would format the container as a string and then writeln that. There are some examples in std.format's documentation of how to make it prettier. I relied on that to do the same for mir's ndslices:
https://github.com/libmir/numir/pull/10
October 09, 2017
On Sunday, 8 October 2017 at 21:48:05 UTC, Fra Mecca wrote:
> Hi,
> I have noticed that there are elements of core and phobos that are pretty ugly when printed via writeln.
>
> One example is container.Array, but also exceptions.
>
> Should we prettify all of them to have a result similar to the one in python?

This has been previously discussed (https://issues.dlang.org/show_bug.cgi?id=13971) and AFAIR the opinions were divided.

For one, the only thing necessary to get pretty-printing of the elements of a container is to slice it. Slicing a container returns a range and since std.format supports printing ranges it works out-of-the box.

import std.stdio, std.container;
void main(string[] args)
{
    auto arr = make!(Array!int)(1, 2, 3);
    writeln(arr);
    writeln(arr[]); // slice
}

Array!int(RefCounted!(Payload, cast(RefCountedAutoInitialize)0)(RefCountedStore(7F10930E8490)))
[1, 2, 3]

https://run.dlang.io/is/WQiV3P

Another point is that if the container contains many elements automatically printing all of them would be ugly, unhelpful and slow, while printing its identity (I think the address of the RefCountedStore doesn't change on reallocation) may be more useful.


October 09, 2017
On Monday, 9 October 2017 at 17:19:42 UTC, Petar Kirov [ZombineDev] wrote:
>
> Another point is that if the container contains many elements automatically printing all of them would be ugly, unhelpful and slow, while printing its identity (I think the address of the RefCountedStore doesn't change on reallocation) may be more useful.

This relates to your comment in the bugzilla to format with '%5r'. This would be similar to using something like head or tail in R and the printing the result. I consider that a separate issue. It's really easy to handle this in D with UFCS. In Mir, you could do slice.select(5).writefln!"%s".
October 09, 2017
On Monday, 9 October 2017 at 17:44:12 UTC, jmh530 wrote:
> On Monday, 9 October 2017 at 17:19:42 UTC, Petar Kirov [ZombineDev] wrote:
>>
>> Another point is that if the container contains many elements automatically printing all of them would be ugly, unhelpful and slow, while printing its identity (I think the address of the RefCountedStore doesn't change on reallocation) may be more useful.
>
> This relates to your comment in the bugzilla to format with '%5r'. This would be similar to using something like head or tail in R and the printing the result. I consider that a separate issue. It's really easy to handle this in D with UFCS. In Mir, you could do slice.select(5).writefln!"%s".

Yes, nowadays I am not so certain that we need a dedicated format for printing only some of the elements, as for most containers it should be as simple as container[0..5] or container[].take(5). What I'm certain though, is that I don't want all my containers with millions of elements to have their elements printed by default. The std.format code could probably hide them if they're too many, but at this point it is better to leave the customization to the user. Printing the container type, address and length would be most useful default IMO (but in a better looking way than what we have now for std.container.array).
October 10, 2017
On Monday, 9 October 2017 at 18:52:03 UTC, Petar Kirov [ZombineDev] wrote:
> On Monday, 9 October 2017 at 17:44:12 UTC, jmh530 wrote:
>> On Monday, 9 October 2017 at 17:19:42 UTC, Petar Kirov [ZombineDev] wrote:
>>>
Printing the container
> type, address and length would be most useful default IMO (but in a better looking way than what we have now for std.container.array).

That's your opinion though; IMO I'd rather have the elements printed by default.

It's all subjective and thus there's no real reason for one or the other.
October 10, 2017
On Tuesday, 10 October 2017 at 11:17:09 UTC, bauss wrote:
> On Monday, 9 October 2017 at 18:52:03 UTC, Petar Kirov [ZombineDev] wrote:
>> On Monday, 9 October 2017 at 17:44:12 UTC, jmh530 wrote:
>>> On Monday, 9 October 2017 at 17:19:42 UTC, Petar Kirov [ZombineDev] wrote:
>>>>
> Printing the container
>> type, address and length would be most useful default IMO (but in a better looking way than what we have now for std.container.array).
>
> That's your opinion though; IMO I'd rather have the elements printed by default.
>
> It's all subjective and thus there's no real reason for one or the other.

FWIW I tried to propose a `dump` function one year ago [1].
There has never been a great interest in it though as while using `writeln` is a bit annoying (e.g. no spaces, no variable names) apparently all core people got used to it already ;-)

If someone considers to add a pretty printing method he/she, this new symbol could also be very useful for single elements too (and thus gain chances in its acceptance).

[1] https://github.com/dlang/phobos/pull/4318#issuecomment-241819997