Garbage collection and closures.

Dsby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 19 02:18:56 PDT 2017


On Monday, 19 June 2017 at 09:10:16 UTC, Dsby wrote:
> On Saturday, 17 June 2017 at 17:15:50 UTC, Adam D. Ruppe wrote:
>> On Saturday, 17 June 2017 at 14:19:34 UTC, ANtlord wrote:
>>> [...]
>>
>> Where the variable is defined that is referenced in the 
>> closure.
>>
>> So:
>>
>> [...]
>
> if the uses parma is 'scope':
>
> void uses(scope void delegate() dg);
>
> will it be not alloc memory?

I test it , if use scope it will not alloc memony.

import std.stdio;

void call(void delegate() fun)
{
     fun();
}

void call2(scope void delegate() fun)
{
     fun();
}

void main()
{
     int a = 10;
    // call((){writeln(a);});
     call2((){writeln(a);});
}
dmd -vgc ./t.d
it will not print anything.

if use call:
void main()
{
     int a = 10;
     call((){writeln(a);});
    // call2((){writeln(a);});
}

dmd -vgc ./t.d                                                    
                                                                   
          182ms  2017年06月19日 星期一 17时16分47秒
./t.d(13): vgc: using closure causes GC allocation



More information about the Digitalmars-d-learn mailing list