www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15891] New: Compiler error when std.algorithm.cache and

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

          Issue ID: 15891
           Summary: Compiler error when std.algorithm.cache and
                    std.algorithm.joiner and map composed
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: zhaopuming gmail.com

I have a range of range of range, which is obtained by two levels of map, I'd
like to flatten it into a simple range, so two joiners are needed.

But I found that joiners are not completely lazy, the front element would be
evaluated (twice!). See discussion here:
http://forum.dlang.org/post/qswryemgvixhpqpybekb forum.dlang.org. So I need a
cache before the joiner to prevent my map function called twice.

But when two levels of cache.joiner and cache.joiner are composed, the compiler
will complain, see a demo code here:

```
import std.stdio : writeln;
import std.algorithm : map, cache, joiner;
import std.array : array;

auto read(int a) {
   return [0, a]; // second level
}

auto mkarray(int a) {
  return [-a, a].map!(x=>read(x)).cache.joiner; // to avoid calling read twice
}

void main() {
  auto xs = [1,2 ,3, 4];
  auto r = xs.map!(x=>mkarray(x)).array;

  // Both lines below should be equal, but second does not compile
  [[0, -1, 0, 1], [0, -2, 0, 2], [0, -3, 0, 3], [0, -4, 0,
4]].cache.joiner.writeln;
  r.cache.joiner.writeln;
}
```

Above results in following error:
/opt/compilers/dmd2/include/std/algorithm/iteration.d(326): Error: one path
skips field __caches_field_0
/d617/f62.d(19): Error: template instance
std.algorithm.iteration.cache!(Result[]) error instantiating

--
Apr 07 2016