Closures in VB Part 2: Method Calls

Jared here again. This part of the series will focus on how method calls are handled in closures. As stated in the previous article, the purpose of closures is to allow all operations inside a lambda or query expression that would normally be available inside the function or sub. To do this closures often need to capture (or lift) relevant variables from the function into the generated class.

There are 2 types of methods and method calls that closures have to handle.

Method calls to a shared method or methods on modules.

Method calls to instance members of a class

Scenario #1

Below is an example of a method call inside a lambda expression for scenario #1.

Module M1

Function MyValue() AsIntegerReturn42EndFunction

Sub Example1()Dim x = 5Dim f = Function() x + MyValue()EndSub

EndModule

Here we are calling a module method inside a lambda. Module Methods or Shared methods can be called from anywhere because they require no specific variable for the call. This requires no special work from closures as the call can just be made naturally.

Class ClosurePrivate x AsInteger

Function Lambda_f() AsIntegerReturn x + M1.MyValueEndFunctionEndClass

Scenario #2

Calling an instance method is more difficult than a shared method because it requires the referenc “Me”. If you don’t type this specifically in code the VB Compiler will add it for you under the hood. To make this work the closures code will also “lift” the variable “Me” in the same way that it lifts normal variables in a function.

Calling a instance method inside a lambda expression is little difference than calling a member method on a variable used in a lambda. The only difference is the variable is “Me”. For example

Class C1Private m_myValue AsInteger

Function MyValue() AsIntegerReturn m_myValueEndFunction

Sub Example2()Dim x = 5Dim f = Function() x + MyValue()EndSubEndClass

In this case we need to access both “x” and “Me.MyValue()” from the closure. The generated code will create space for both of these variables and the transformed code in Example2 will store both of the values.

As usual, the generated code is much uglier but this essentially what will be generated. That wraps it up for method calls. In the next part, I will discuss the variable liftetime and scoping issues that come into play with closures.