digitalmars.D.learn - Why does BinaryHeap sometime cause compile-error in foeach?
- Shigeki Karita (16/16) Sep 30 2017 https://dpaste.dzfl.pl/cd605899d050
- user1234 (5/21) Sep 30 2017 The reason why it doesn't work is much more simple than you
- Shigeki Karita (1/1) Sep 30 2017 Oh, struct/class semantics really confuses me!
https://dpaste.dzfl.pl/cd605899d050 why this code cannot convert to foreach (over Structs and Classes with Ranges). auto h = new BinaryHeap!(int[])(new int[0]); typeof(h).stringof.writeln; static assert(isInputRange!(typeof(h))); h.insert(3); h.insert(1); h.insert(2); // Error: invalid foreach aggregate `h` // foreach (e; h) e.writeln; for (; !h.empty; h.popFront()) { auto e = h.front(); e.writeln; } https://dlang.org/spec/statement.html#foreach-with-ranges
Sep 30 2017
On Saturday, 30 September 2017 at 09:27:23 UTC, Shigeki Karita wrote:https://dpaste.dzfl.pl/cd605899d050 why this code cannot convert to foreach (over Structs and Classes with Ranges). auto h = new BinaryHeap!(int[])(new int[0]); typeof(h).stringof.writeln; static assert(isInputRange!(typeof(h))); h.insert(3); h.insert(1); h.insert(2); // Error: invalid foreach aggregate `h` // foreach (e; h) e.writeln; for (; !h.empty; h.popFront()) { auto e = h.front(); e.writeln; } https://dlang.org/spec/statement.html#foreach-with-rangesThe reason why it doesn't work is much more simple than you think: h is not a BinaryHeap, it's a pointer to. Try "foreach (e; *h) e.writeln;" and it works.
Sep 30 2017
Oh, struct/class semantics really confuses me!
Sep 30 2017