chunkBy limitation?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 2 11:16:58 PDT 2015
On 10/02/2015 02:21 AM, Dmitri wrote:
> I wondered if this is a current limitation of chunkBy implementation:
>
> // http://dpaste.dzfl.pl/52d6a0c5d0ab
> void bar(int[] foo)
> {
> bool less(int a, int b) // contrived
> {
> return a < b;
> };
>
> foo.sort!less.groupBy;
> }
>
> resulting in:
>
> /opt/compilers/dmd2/include/std/algorithm/iteration.d(1529): Error:
> function f351.bar.SortedRange!(int[],
> less).SortedRange.groupBy!().groupBy.ChunkByImpl!(__lambda1,
> int[]).ChunkByImpl.Group.popFront cannot access frame of function
> f351.bar.SortedRange!(int[], less).SortedRange.groupBy!().groupBy
> ...
>
> I can see that the implementation of chunkBy is nested:
> struct ChunkByImpl
> {
> static struct Group {}
> }
>
> and that the predicate (less in this case) is accessed from the nested
> struct Group which is, probably, what's causing the compilation error.
>
> The question is if this is an implementation error (i.e. Group should
> probably be moved to the top level) or a compiler issue?
>
> Thanks in advance.
This is a known D issue. Currently, objects have a single context
pointer. Here is a bug discussion about it:
https://issues.dlang.org/show_bug.cgi?id=5710
A workaround for your example is making less() static, which removes its
context pointer:
void bar(int[] foo)
{
import std.algorithm : sort;
static bool less(int a, int b) // contrived
{
return a < b;
}
foo.sort!less.groupBy;
}
void main() {}
I've just realized that I don't know how to handle the case where less()
really needs some other state (e.g. it may need to use a local variable
in main). What can we do?
Ali
More information about the Digitalmars-d-learn
mailing list