[Issue 13971] Writing a container should write its default range

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jan 17 08:51:20 PST 2015


https://issues.dlang.org/show_bug.cgi?id=13971

Petar Kirov <petar.p.kirov at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |petar.p.kirov at gmail.com

--- Comment #3 from Petar Kirov <petar.p.kirov at gmail.com> ---
IMO, this is a nice feature, but it's not a big deal that it doesn't work
currently. For example the following in C#:

var l = new List<int> { 2, 6, 8, 4, 1 };
Console.WriteLine(l);

Prints:
System.Collections.Generic.List`1[System.Int32]

I think that %s should print something like Array!int at 20D9590, thus hiding the
implementation details and only revealing the identity (the address) of the
container. The %s should print contents only if you provide a range (built-in
arrays and AAs are more of a range, IMO).

Perhaps we should introduce a new format spec for printing the contents of
containers, like %r (or a more appropriate letter). E.g.:

import std.container, std.range, std.algorithm, std.stdio;
void main()
{
  Array!int arr;
  iota(100).each!(i => arr.insertBack(i));
  writefln("%5r", arr);
}

Should print:
[0, 1, 2, 3, 4]

Where the '5' in %5r is the FormatSpec width and means print the first 5
elements.

--


More information about the Digitalmars-d-bugs mailing list