How to create delegates with an independent scope?
vit
vit at vit.vit
Wed Mar 30 13:09:55 UTC 2022
On Wednesday, 30 March 2022 at 12:56:39 UTC, Vijay Nayar wrote:
> On Wednesday, 30 March 2022 at 12:53:10 UTC, vit wrote:
>> use two delegates :)
>>
>> ```d
>> (){
>> // Deliberately create a copy to keep in delegate scope.
>> string myStr = i.dup;
>> // The delegate will hopefully carry this copy around in
>> its own scope.
>> funcs ~= (() => myStr ~ " sandwich");
>> }();
>> ```
>
> Very interesting. Both this and creating a "function creator
> function" work, and it seems clear that functions create their
> own scopes. However, it seems that loops do not, is that
> correct? Maybe I was thrown off by the surrounding `{ }`, but
> I had assumed that loops created their own scopes.
It is bug: https://issues.dlang.org/show_bug.cgi?id=21929
More information about the Digitalmars-d-learn
mailing list