GC allocation

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 21 12:54:10 PDT 2016


I'm on mobile so I will be brief now and expand later

On Thu, Apr 21, 2016 at 07:37:59PM +0000, QAston via Digitalmars-d-learn wrote:
> Just like classes - when closure expression is executed.

Heap closures are actually allocated on declaration. The compiler
looks to see if it will need to be copied and if so, avoids it by just
using the heap to begin with.

> Instead of using a delegate you can use a Struct with opCall.

Indeed, though delegates do not necessarily allocate. If they come from
&obj.member, they never do. If the usage point calls them scope, they never do.
If the usage is an alias arg and it can be inlined, it might not.

However, if you do &obj.member you do need to be sure obj stays alive while
the delegate is in use, so struct w/ opCall may make lifetime management easier.



More information about the Digitalmars-d-learn mailing list