Array of byLineCopy ranges behaves as they are byLine

HaraldZealot harald_zealot at tut.by
Mon Mar 11 15:23:53 UTC 2019


Let assume I have multiple files (like "file1", "file2" and so 
on). I need to iterate them simultaneously with some logic. I 
have tried to create random access range of ranges, however, it 
behaves not as expected. Let "file1" is
```
1
2
3
4
```

Then this code
```d
File[] files;
foreach(filename; args[1 .. $])
{
     files ~= File(filename, "r");
}

auto ranges = files.map!(a => a.byLineCopy);

writeln(ranges[0].front);
writeln(ranges[0].front);
writeln(ranges[0].front);
```
produces
```
1
2
3
```
Even despite I'm using `byLineCopy`.

However, this code
```d
File[] files;
foreach(filename; args[1 .. $])
{
     files ~= File(filename, "r");
}

auto range = files[0].byLineCopy;

writeln(range.front);
writeln(range.front);
writeln(range.front);
```
produses
```
1
1
1
```
as expected.

What I'm doing wrong with `map`? Or is this a bug?

dmd v2.085.0

P.S. I know that I can call `dup` on `front` and I'm going to do 
so as workaround. However, I'm curious why current situation is 
taking place.


More information about the Digitalmars-d-learn mailing list