How to create delegates with an independent scope?

Vijay Nayar madric at gmail.com
Wed Mar 30 12:56:39 UTC 2022


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.


More information about the Digitalmars-d-learn mailing list