How to provide this arg or functor for algorithm?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 16 15:22:06 PDT 2015


On 08/16/2015 09:26 AM, FreeSlave wrote:

 >> It still says it needs allocation:
 >>
 >> test.d(17): Error: function test.func @nogc function allocates a
 >> closure with the GC

I wrapped it inside a class object but it still thinks it needs to allocate:

import std.stdio;
import std.range;
import std.algorithm;

struct Caller
{
     this(uint context) {
         _context = context;
     }
     uint method(uint value) {
         return _context * value;
     }

     uint _context;
}

class DelegateRef
{
     uint delegate(uint) d;

     this (uint delegate(uint) d)
     {
         this.d = d;
     }
}

// HERE:
// Error: function deneme.func @nogc function allocates
//        a closure with the GC
@nogc auto func(uint[] arr, DelegateRef d)
{
     return arr.map!(a => d.d(a));
}

void main(string[] args)
{
     uint[] arr = [1,2,3];
     uint context = 2;
     auto c = Caller(context);
     auto d = &c.method;

     writeln(func(arr, new DelegateRef(d)));
}

Ali



More information about the Digitalmars-d-learn mailing list