closures

Jarrett Billingsley jarrett.billingsley at gmail.com
Thu Jul 16 11:56:28 PDT 2009


On Thu, Jul 16, 2009 at 2:22 PM, Steven
Schveighoffer<schveiguy at yahoo.com> wrote:
>
> What about syntax to force closure behavior (on or off)?  Not that I have
> any to suggest, but if the compiler is going to remain ignorant about escape
> analysis, then we should be able to supply the intelligence, and hacking an
> extra wrapper function to force behavior seems... well, hackish :)

There already is, at least for turning closures off.  A common design
pattern in D1 is to pass the address of a nested function as a
callback.  This is convenient and performant:

void putChar(char c)
{
    write(c);
}

std.format.doFormat(&putChar, blahblahblah); // or so

If D2 were to allocate a closure for &putChar, suddenly your
formatting function that's called several hundred times a second
starts eating memory.  The solution is to put 'scope' on the
parameter:

void doFormat(scope void delegate(char) dg, blahblah) { .. }

DMD2 will not allocate a closure for the initial code if 'scope' is
present, and if removed, it will.


More information about the Digitalmars-d-learn mailing list