Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.


Closures in VB: Part 1

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...).

Published Friday, April 27, 2007 6:50 PM by Jared Parsons

Filed under: , , ,

Comments

# Closures in VB Part 2: Method Calls @ Thursday, May 03, 2007 5:56 PM

For previous articles in this series, please see Part 1 - The basics This part of the series will focus

jaredpar's WebLog

# re: Closures in VB: Part 1 @ Wednesday, May 16, 2007 7:01 PM

That is nice.  So now that we have functions as types, we are one step closer to closing the gap on Lisp.  :D

Robz

# Closures in VB Part 3: Scope @ Friday, May 25, 2007 4:59 PM

For previous articles in this series please see Part 1: Introduction Part 2: Method Calls Thus far in

jaredpar's WebLog

# Closures in VB Part 4: Variable Lifetime @ Friday, June 15, 2007 4:47 PM

For previous articles in this series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope

jaredpar's WebLog

# Closures in VB Part 5: Looping @ Thursday, July 26, 2007 12:39 PM

For previous articles in the series please see Part 1: Introduction Part 2: Method Calls Part 3: Scope

jaredpar's WebLog

# Closures in VB Part 6: Limitations @ Monday, August 06, 2007 6:36 PM

For previous articles in this series please see ... Part 1: Introduction Part 2: Method Calls Part 3:

jaredpar's WebLog

# Closures in VB Part 6: Limitations @ Monday, August 06, 2007 7:16 PM

For previous articles in this series please see ... Part 1: Introduction Part 2: Method Calls Part 3

Noticias externas

New Comments to this post are disabled
Page view tracker