On Tuesday, 11 December 2012 at 15:22:49 UTC, Ali Çehreli wrote:
That used to work a couple of dmd versions ago. I think it was a bug that it worked, so it stopped working after bug fixes.

If I'm not mistaken this is actually related to a compiler implementation issue: Lambda's have a single pointer to store the context that they have been started in.

When a lambda is a free-standing function (aka "module function" or "global function") then there is only the context to deal with. When the template is a member function (taskPool.map is) then there is also the object that the function is started on.

The single pointer of the lambda is not sufficient to store both without big changes in the compiler.

(I may be off with that description above. e.g. there may be two pointers when three are actually needed, etc.)

I had to change following chapter after dmd's behavior had changed:

  http://ddili.org/ders/d.en/parallelism.html

--- Quoting ---
import std.parallelism;
// ...
double averageGrade(Student student)
{
    return student.averageGrade;
}
// ...
    auto results = taskPool.map!averageGrade(students, 3);

Note: The free-standing averageGrade() function above is needed due to a limitation that involves using local delegates with member function templates like TaskPool.map:

auto results = taskPool.map!(a => a.averageGrade)(students, 3); // ← compilation ERROR
----------

As you see above, the solution is to use a function with taskPool.map, not a lambda.

Ali

I try to use a function instead of a lambda function I'm keep getting compiler errors. Code :
      Entity MapIntegrator (ref Entity me) {
        me.Integrador3Orden (iDeltaT);
        return me;
      }
      objects = array( taskPool.map!MapIntegrator(objects) );

With taskPool.Map I get this errors :
simulator.d(196): Error: template instance map!(MapIntegrator) cannot use local 'MapIntegrator' as parameter to non-global template map(functions...) /usr/include/dmd/phobos/std/parallelism.d(1969): Error: function std.parallelism.TaskPool.map!(MapIntegrator).map!(Entity[]).map.Map.fillBuf cannot access frame of function D main /usr/include/dmd/phobos/std/parallelism.d(1974): Error: template instance amap!(MapIntegrator) cannot use local 'MapIntegrator' as parameter to non-global template amap(functions...) /usr/include/dmd/phobos/std/parallelism.d(1675): Error: function std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]).amap cannot access frame of function D main /usr/include/dmd/phobos/std/parallelism.d(1706): Error: function std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]).amap.doIt cannot access frame of function D main /usr/include/dmd/phobos/std/parallelism.d(1974): Error: template instance std.parallelism.TaskPool.amap!(MapIntegrator).amap!(Entity[],ulong,Entity[]) error instantiating
simulator.d(196):        instantiated from here: map!(Entity[])
simulator.d(196): Error: template instance std.parallelism.TaskPool.map!(MapIntegrator).map!(Entity[]) error instantiating
make: *** [predSim] Error 1

But again, with std.algorthim Map it don give any error and works fine.

Reply via email to