Printing a range of ranges drains them
Salih Dincer
salihdb at hotmail.com
Mon May 27 10:15:19 UTC 2024
On Monday, 27 May 2024 at 06:31:37 UTC, Jonathan M Davis wrote:
> ...
> Given that writeln needs to work with basic input ranges,
> having it not consume the inner ranges would result in
> different behavior between basic input ranges and forward
> ranges, which wouldn't be great. So, arguably, having it
> consume them is the correct choice, but I'd have to spend a
> fair bit of time thinking through the implications to come to a
> properly informed conclusion.
> ...
It is possible to show the same situation with iota() and of
course if this is a contradictory situation:
```d
alias strings = char[][];
enum form = "[%(%s, %)]";
void main()
{
auto num = iota(1, 4);
auto range = [num, num, num];
write("[ ");
foreach(rng; range)
rng.write(" ");
writeln("]");
range.writefln!form;
range.writefln!form;
strings str;
auto s = "123".dup;
str = [s, s, s];
str.writefln!form;
str.writefln!form;
} /*
[ [1, 2, 3] [1, 2, 3] [1, 2, 3] ]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[], [], []]
["123", "123", "123"]
["123", "123", "123"]
//*/
```
When we do the same experiment with the strings above, we get
different results. Moreover, foreach() similarly does not consume
the inner ranges...
Now there is a contradiction on the writeln() side!
SDB at 79
More information about the Digitalmars-d
mailing list