Bug 15891 – Compiler error when std.algorithm.cache after 2 layers of std.algorithm.map

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-04-07T14:24:33Z
Last change time
2020-01-13T07:37:26Z
Keywords
pull
Assigned to
No Owner
Creator
zhaopuming

Comments

Comment #0 by zhaopuming — 2016-04-07T14:24:33Z
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/[email protected]. 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
Comment #1 by issues.dlang — 2016-04-08T01:17:46Z
r.cache.writeln fails on its own. This looks like it's a problem specifically with cache and not joiner, though it's not clear to me exactly what's going wrong. It looks like it's complaining that caches[0] and caches[1] are assigned values in one branch of the constructor and not another, but I have no idea why that would be an error. I've never seen anything like that before.
Comment #2 by bugzilla — 2019-12-04T12:00:31Z
reduced example: ``` import std.algorithm; void main() { [1].map!(x=>[x].map!(y=>y)).cache; } ```
Comment #3 by dlang-bot — 2019-12-04T13:18:54Z
@berni44 created dlang/phobos pull request #7302 "Fix Issue 15891 - Compiler error when std.algorithm.cache after 2 layers of std.algorithm.map" fixing this issue: - Fix Issue 15891 - Compiler error when std.algorithm.cache after 2 layers of std.algorithm.map https://github.com/dlang/phobos/pull/7302
Comment #4 by dlang-bot — 2020-01-13T07:37:26Z
dlang/phobos pull request #7302 "Fix Issue 15891 - Compiler error when std.algorithm.cache after 2 layers of std.algorithm.map" was merged into master: - 66a2ba02fdfd5d80170592bdb1269c6172342c22 by Bernhard Seckinger: Fix Issue 15891 - Compiler error when std.algorithm.cache after 2 layers of std.algorithm.map https://github.com/dlang/phobos/pull/7302