I've seen countless posts on how variable capture pulls in variables for the creation of the closure, however they all seem to stop short of specific details and call the whole thing "compiler magic".
I'm looking for a clear-cut explanation of:
My preference would be for an answer in terms of values and pointers (closer to the heart of what happens internally), though I will accept a clear answer involving values and references as well.
Variable capture occurs when macroexpansion causes a name clash: when some symbol ends up referring to a variable from another context. Inadvertent variable capture can cause extremely subtle bugs. This chapter is about how to foresee and avoid them.
Capturing Values. A closure can capture constants and variables from the surrounding context in which it's defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.
In Swift, closures capture the variables they reference: variables declared outside of the closure but that you use inside the closure are retained by the closure by default, to ensure they are still alive when the closure is executed.
Capture lists: The list of values that you want to remain unchanged at the time the closure is created. Capturing values: If you use external values inside your closure, Swift stores them alongside the closure. This way they persist even when the external part of the code no longer exists.
It's probably easiest to demonstrate how the capturing works via an example...
Here's some code using a lambda expression which captures a single variable:
using System; class Test { static void Main() { Action action = CreateShowAndIncrementAction(); action(); action(); } static Action CreateShowAndIncrementAction() { Random rng = new Random(); int counter = rng.Next(10); Console.WriteLine("Initial value for counter: {0}", counter); return () => { Console.WriteLine(counter); counter++; }; } }
Now here's what the compiler's doing for you - except that it would use "unspeakable" names which couldn't really occur in C#.
using System; class Test { static void Main() { Action action = CreateShowAndIncrementAction(); action(); action(); } static Action CreateShowAndIncrementAction() { ActionHelper helper = new ActionHelper(); Random rng = new Random(); helper.counter = rng.Next(10); Console.WriteLine("Initial value for counter: {0}", helper.counter); // Converts method group to a delegate, whose target will be a // reference to the instance of ActionHelper return helper.DoAction; } class ActionHelper { // Just for simplicity, make it public. I don't know if the // C# compiler really does. public int counter; public void DoAction() { Console.WriteLine(counter); counter++; } } }
If you capture variables declared in a loop, you'd end up with a new instance of ActionHelper
for each iteration of the loop - so you'd effectively capture different "instances" of the variables.
It gets more complicated when you capture variables from different scopes... let me know if you really want that sort of level of detail, or you could just write some code, decompile it in Reflector and follow it through :)
Note how:
EDIT: Here's an example of two delegates sharing a variable. One delegate shows the current value of counter
, the other increments it:
using System; class Program { static void Main(string[] args) { var tuple = CreateShowAndIncrementActions(); var show = tuple.Item1; var increment = tuple.Item2; show(); // Prints 0 show(); // Still prints 0 increment(); show(); // Now prints 1 } static Tuple<Action, Action> CreateShowAndIncrementActions() { int counter = 0; Action show = () => { Console.WriteLine(counter); }; Action increment = () => { counter++; }; return Tuple.Create(show, increment); } }
... and the expansion:
using System; class Program { static void Main(string[] args) { var tuple = CreateShowAndIncrementActions(); var show = tuple.Item1; var increment = tuple.Item2; show(); // Prints 0 show(); // Still prints 0 increment(); show(); // Now prints 1 } static Tuple<Action, Action> CreateShowAndIncrementActions() { ActionHelper helper = new ActionHelper(); helper.counter = 0; Action show = helper.Show; Action increment = helper.Increment; return Tuple.Create(show, increment); } class ActionHelper { public int counter; public void Show() { Console.WriteLine(counter); } public void Increment() { counter++; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With