How to provide this arg or functor for algorithm?
FreeSlave via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 16 09:26:16 PDT 2015
On Sunday, 16 August 2015 at 16:23:05 UTC, FreeSlave wrote:
> On Sunday, 16 August 2015 at 15:29:10 UTC, Ali Çehreli wrote:
>> On 08/16/2015 04:53 AM, FreeSlave wrote:
>>
>> > The problem is that this allocates delegate, so it can't be
>> used in
>> > @nogc code.
>>
>> Would constructing the delegate by setting its .funcptr and
>> .ptr properties work in this case? You can have a pool of
>> context objects which become the context for the delegate.
>>
>> http://ddili.org/ders/d.en/lambda.html#ix_lambda..funcptr
>>
>> Ali
>
> I don't see how this can solve the problem.
>
> What I tried:
>
> 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;
> }
>
> @nogc auto func(uint[] arr, uint function(uint) f)
> {
> return arr.map!(f);
> }
>
> void main(string[] args)
> {
> uint[] arr = [1,2,3];
> uint context = 2;
> auto c = Caller(context);
> auto d = &c.method;
> writeln(func(arr, d.funcptr));
> }
>
> It still says it needs allocation:
>
> test.d(17): Error: function test.func @nogc function allocates
> a closure with the GC
>
> funcptr does not play any role here, since passing the delegate
> directly leads to the same error.
Forgot about data ptr.
@nogc auto func(uint[] arr, uint function(uint) f, void* data)
{
uint delegate(uint) d;
d.funcptr = f;
d.ptr = data;
return arr.map!(d);
}
void main(string[] args)
{
uint[] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = &c.method;
writeln(func(arr, d.funcptr, d.ptr));
}
Still the same error though.
More information about the Digitalmars-d-learn
mailing list