Closures in VB: Part 1

Closures in VB: Part 1

  • Comments 10

 

My name is Jared Parsons and I am a developer on the VB Compiler and Debugger.  One of the features I implemented for VB 9.0 is lexical closure support.  This a great addition to the VB language and I wanted to do a series of blog posts to describe this feature and how it will impact your code.

Lexical Closures (more often referred to as simply Closures) are the underpinnings for several new features in Visual Basic 9.0.  The are part of the guts of Lambda and Query expressions.  This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use. 

To start off, let's get a basic summary of what a Closure is.  Wikipedia defines it as "... a  is a semantic concept referring to a function paired with an environment ...".  I prefer to describe it as follows.  A closure is a feature which allows users to seemlessly access an environment (locals, parameters and methods) from more than one function.  Even better are samples :)

    Class C1
        Sub Test()
            Dim x = 5
            Dim f = Function(ByVal y As Integer) x + y
            Dim result = f(42)
        End Sub

    End Class

In this code we have a lambda expression which takes in a single parameter and adds it with a local variable.  Lambda expressions are implemented as functions in VB (and C#).  So now we have two functions, "Test" and "f", which are accessing a single local variable.  This is where closures come into play.  Closures are responsible for making the single variable "x" available to both functions in a process that is referred to as "lifting the variable".

To do this the compiler will take essentially 4 actions. 

  1. Create a class which will contain "x" in order to share it among both functions.  Call it "Closure" for now
  2. It will create a new function for the lambda expression in the class "Closure".  Call it "f" for now
  3. Create a new instance of the class "Closure" inside the sub "Test"
  4. Rewrite all access of "x" into the member "x" of "Closure".
    Class Closure
        Public x As Integer

        Function f(ByVal y As Integer) As Integer
            Return x + y
        End Function
    End Class

    Class C1
        Sub Test()
            Dim c As New Closure()
            c.x = 5
            Dim f As Func(Of Integer, Integer) = AddressOf c.f
            Dim result = f(42)
        End Sub

    End Class

Now "x" is shared amongst both functions and the user didn't have to know anything about the code we generated.  You can see from this simplified example just how much code Closures and all of the other new VB 9.0 features are saving you here (Type Inference, Lambda Expressions). 

Note this is only a simulation of what is generated when you use a closure, the actual generated code is much uglier and involves lots of unbindable names "$Lambda_1", etc ...

In the next part of this article I'll dive into some more uses of closures (multiple variables, method access,  terminology, etc...).

Jared Parsons

https://blogs.msdn.com/jaredpar

Leave a Comment
  • Please add 8 and 8 and type the answer here:
  • Post
  • Closures in VB: Part 1(The Visual Basic Team)より 今の OrcasBeta1 ではまだ VB の ラムダ式 はサポートされていないんだけれど、先回りで注意だけ。

  • For previous articles in this series, please see Part 1 - The basics Jared here again. This part of the

  • hello

    im an irania man that i wrote a programme like nero but a area of this programme faced problem . my problem is this that i cant write a function in vb.

    this function is very very hard for me.

    i wanna a function that write information in cd

    and erase cd after writing

    just this 2 function

    for this reqest i will spend every mony that u want of me

    iranian cant write this function

    and i refer to you.

    note : this function must writed in vb _ visual basic

    thanks alot

    my name is mousa basiratnia

    my Id :  0123 3112284

    -----------------------------------------------------------------

    excuse me for this that i cant speak english very good

    because im poor in english

    ----------------------------------

  • Jared here again. For previous articles in this series please see Part 1: Introduction Part 2: Method

  • 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

  • [ 原文作者 ] : Jared Parsons [ 原文链接 ] : Closures in VB: Part 1 我叫 Jared Parsons ,是微软VB 编译和调试组的软件开发人员。我为VB

  • please help me in about write cd/dvd to visual basic 6.0

  • Any chance you could update the posts in this series with links to the rest of the series - saves a fair bit of effort hunting for them.

    Thanks

Page 1 of 1 (10 items)