A group blog from members of the VB team
Jared here again. For previous articles in this series please see
Thus far in the series we've only lifted variables that are declared in the same block/scope. What happens if we lift variables in different scope? The answer is that one closure class will be created for every unique scope where a lifted variable is declared and all of the variables in that scope that are lifted will be placed in that closure. Once again, examples speak best
Sub Scope1() Dim x = 5 Dim f1 = Function(ByVal z As Integer) x + z Console.WriteLine(f1(5)) If x > 2 Then Dim y = 6 Dim g = 7 Dim f2 = Function(ByVal z As Integer) z + y + g Console.WriteLine(f2(4)) End If End Sub
The code will end up looking like so ...
Class Closure1 Public x As Integer Function Lambda_f1(ByVal z As Integer) Return x + z End Function End Class Class Closure2 Public y As Integer Public g As Integer Function Lambda_f2(ByVal z As Integer) Return y + z + g End Function End Class Sub Scope1() Dim c1 As New Closure1() c1.x = 5 Console.WriteLine(c1.Lambda_f1(5)) If c1.x > 2 Then Dim c2 As New Closure2() c2.y = 6 c2.g = 7 Console.WriteLine(c2.Lambda_f2(4)) End If End Sub
There are a couple of items to take away from this example.
The next twist is what were to happen if the lambda "f2" were to also use the variable "x". As it's currently written there is no association between Closure1 and Closure2 therefore there is no way for it to access the lifted variable. The answer is two fold. Firstly to reduce clutter I pasted the closure classes as if they were separate entries. In fact Closure2 would appear as a nested class of Closure1 in the real generated code.
Secondly if x were used inside of "f2", the real use would be "c1.x". That's (almost) no different than "someOtherVar.x". Therefore the instance of c1 will be lifted into Closure2.
Dim f2 = Function(ByVal z As Integer) z + y + g + x
Woud result in the following definition of Closure2 ...
Class Closure2 Public y As Integer Public g As Integer Public c1 As Closure1 Function Lambda_f2(ByVal z As Integer) Return y + z + g + c1.x End Function End Class
In deeply nested lambdas and scopes this type of lifting will continue recursively.
That's it for this entry, the next article will talk about looping structures and possibly variable lifetime.
A great series of articles - thanks for the background detail as I think it is fascinating to see how things like Closures are implemented within VB and the .NET Framework. The potential for such abstraction is one of the most interesting capabilities of .NET.
This post assumes that you understand how closures are implemented in C#. They're implemented in essentially
Can you post a code example of why using closures is beneficial and powerful?
For previous articles in this series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope
For previous articles in the series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope
One of my favorite new features for Code Analysis in Visual Studio 2008 is our support for analyzing