Bug 13971 – Writing a container should write its default range

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-12T16:26:11Z
Last change time
2024-12-01T16:23:32Z
Assigned to
No Owner
Creator
Andrei Alexandrescu
See also
https://issues.dlang.org/show_bug.cgi?id=3248
Moved to GitHub: phobos#10110 →

Comments

Comment #0 by andrei — 2015-01-12T16:26:11Z
See the second snippet at http://maikklein.github.io/2015/01/11/Evaluating-D-for-games/: import std.stdio; import std.container; void main() { Array!int arr; arr.insertBack(10); writeln(arr); } That writes: Array!int(RefCounted!(Payload, cast(RefCountedAutoInitialize)0)(RefCountedStore(20D9590))) We should make writeln(arr) equivalent to writeln(arr[]).
Comment #1 by destructionator — 2015-01-12T18:44:52Z
I'm not sure this should be in writeln btw, I think Array and friends should just have a toString(sink) function that forwards to the range. We could provide a phobos mixin template forwardToStringTo!(member) to keep it DRY.
Comment #2 by kozzi11 — 2015-01-13T19:45:47Z
It could be done easily by add few lines of code to std.format.formatValue. Adding something like this: else static if (__traits(compiles, val[]) && isInputRange!(typeof(val[]))) { auto refVal = val[]; formatRange(w, refVal, f); } to void formatValue(Writer, T, Char)(Writer w, auto ref T val, ref FormatSpec!Char f) if ((is(T == struct) || is(T == union)) && (hasToString!(T, Char) || !is(BuiltinTypeOf!T)) && !is(T == enum))
Comment #3 by petar.p.kirov — 2015-01-17T16:51:20Z
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@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.
Comment #4 by bugzilla — 2021-03-24T20:12:48Z
Meanwhile (with DMD64 D Compiler v2.096.0-rc.1-22-g6d8985b48) the output of Andreis snippet is Array!int(Payload(1, [10]))
Comment #5 by robert.schadek — 2024-12-01T16:23:32Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10110 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB