chunkBy limitation?

2015-10-02 Thread Dmitri via Digitalmars-d-learn
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.


Re: chunkBy limitation?

2015-10-02 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: chunkBy limitation?

2015-10-02 Thread Dmitri via Digitalmars-d-learn

On Friday, 2 October 2015 at 18:16:59 UTC, Ali Çehreli wrote:

On 10/02/2015 02:21 AM, Dmitri wrote:
> [...]
implementation:
> [...]
Error:
> [...]
function
> [...]
less).SortedRange.groupBy!().groupBy
> [...]
the nested
> [...]
compilation error.
> [...]
Group should
> [...]

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


Thanks, Ali - this is helpful. The problem was figuring out what 
exactly is wrong given a bunch of unhelpful error messages. A 
somewhat better message comes when using a unary predicate 
version of chunkBy.