Should we have prettier string printing for exceptions and std elemts?
Petar
Petar
Mon Oct 9 17:19:42 UTC 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.
More information about the Digitalmars-d
mailing list