Nested delegates and closure allocations

FeepingCreature feepingcreature at gmail.com
Tue Jan 16 13:45:22 UTC 2024


On Tuesday, 16 January 2024 at 10:56:58 UTC, Anonymouse wrote:
> I'm increasingly using nested delegates to partition code.
>
> ```d
> void foo(Thing thing)
> {
>     void sendThing(const string where, int i)
>     {
>         send(thing, where, i);
>     }
>
>     sendThing("bar", 42);
> }
> ```
>
> ...
>> 3. Those referenced stack variables that make up the closure 
>> are allocated on the GC heap, unless:
>>
>> * The closure is passed to a scope parameter.
>> * The closure is an initializer for a scope variable.
>> * The closure is assigned to a scope variable.
>
> I'm generally not storing the delegates or passing them around 
> as values, so I don't think the thing about scope variables and 
> parameters *directly* applies.
>
> Am I safe as long as I don't do something like, pass 
> `&sendThing` as an argument to `std.concurrency.receive`?

Yes.


More information about the Digitalmars-d-learn mailing list