{"id":6153,"date":"2007-05-02T23:53:00","date_gmt":"2007-05-02T23:53:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2007\/05\/02\/closures-in-vb-part-1\/"},"modified":"2024-07-05T14:47:02","modified_gmt":"2024-07-05T21:47:02","slug":"closures-in-vb-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/closures-in-vb-part-1\/","title":{"rendered":"Closures in VB: Part 1"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>My name is Jared Parsons and I am a developer on the VB Compiler and Debugger.&nbsp; One of the features I implemented for VB 9.0 is lexical closure support.&nbsp; 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.<\/p>\n<p>Lexical Closures (more often referred to as simply Closures) are the underpinnings for several new features in Visual Basic 9.0.&nbsp; The are part of the guts of Lambda and Query expressions.&nbsp; This will be a several part series on Closures in VB 9.0; how they work, their limitations, pitfalls surrounding their use.&nbsp; <\/p>\n<p>To start off, let&#8217;s get a basic summary of what a Closure is.&nbsp; <a href=\"http:\/\/en.wikipedia.org\/wiki\/Closure_%28computer_science%29\" target=\"_blank\" rel=\"noopener\">Wikipedia<\/a> defines it as &#8220;&#8230; a&nbsp; is a semantic concept referring to a function paired with an environment &#8230;&#8221;.&nbsp; I prefer to describe it as follows.&nbsp; A closure is a feature which allows users to seemlessly access an environment (locals, parameters&nbsp;and methods) from more than one function.&nbsp; Even better are samples \ud83d\ude42 <\/p>\n<pre>    <span>Class<\/span> C1\n        <span>Sub<\/span> Test()\n            <span>Dim<\/span> x = <span>5<\/span>\n            <span>Dim<\/span> f = <span>Function<\/span>(<span>ByVal<\/span> y <span>As<\/span> <span>Integer<\/span>) x + y\n            <span>Dim<\/span> result = f(<span>42<\/span>)\n        <span>End<\/span> <span>Sub<\/span>\n    <span>End<\/span> <span>Class<\/span><\/pre>\n<p>In this code we have a lambda expression which takes in a single parameter and adds it with a local variable.&nbsp; Lambda expressions are implemented as functions in VB (and C#).&nbsp; So now we have two functions, &#8220;Test&#8221; and &#8220;f&#8221;, which are accessing a single local variable.&nbsp; This is where closures come into play.&nbsp; Closures are responsible for making the single variable &#8220;x&#8221; available to both functions in a process that is referred to as &#8220;lifting the variable&#8221;.<\/p>\n<p>To do this the compiler will take essentially&nbsp;4 actions.&nbsp; <\/p>\n<ol>\n<li>Create a class which will contain &#8220;x&#8221; in order to share it among both functions.&nbsp; Call it &#8220;Closure&#8221; for now\n<li>It will create a new function for the lambda expression in the class &#8220;Closure&#8221;.&nbsp; Call it &#8220;f&#8221; for now\n<li>Create a new instance of the class &#8220;Closure&#8221; inside the sub &#8220;Test&#8221;\n<li>Rewrite all access of &#8220;x&#8221; into the member &#8220;x&#8221; of &#8220;Closure&#8221;.<\/li>\n<\/ol>\n<pre>    <span>Class<\/span> Closure\n        <span>Public<\/span> x <span>As<\/span> <span>Integer<\/span>\n        <span>Function<\/span> f(<span>ByVal<\/span> y <span>As<\/span> <span>Integer<\/span>) <span>As<\/span> <span>Integer<\/span>\n            <span>Return<\/span> x + y\n        <span>End<\/span> <span>Function<\/span>\n    <span>End<\/span> <span>Class<\/span>\n    <span>Class<\/span> C1\n        <span>Sub<\/span> Test()\n            <span>Dim<\/span> c <span>As<\/span> <span>New<\/span> Closure()\n            c.x = <span>5<\/span>\n            <span>Dim<\/span> f <span>As<\/span> Func(Of <span>Integer<\/span>, <span>Integer<\/span>) = <span>AddressOf<\/span> c.f\n            <span>Dim<\/span> result = f(<span>42<\/span>)\n        <span>End<\/span> <span>Sub<\/span>\n    <span>End<\/span> <span>Class<\/span><\/pre>\n<p>Now &#8220;x&#8221; is shared amongst both functions and the user didn&#8217;t have to know anything about the code we generated.&nbsp; 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).&nbsp; <\/p>\n<p>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 &#8220;$Lambda_1&#8221;, etc &#8230; <\/p>\n<p>In the next part of this article I&#8217;ll dive into some more uses of closures (multiple variables, method access, &nbsp;terminology, etc&#8230;).<\/p>\n<p>Jared Parsons<\/p>\n<p><a href=\"https:\/\/blogs.msdn.com\/jaredpar\">https:\/\/blogs.msdn.com\/jaredpar<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; My name is Jared Parsons and I am a developer on the VB Compiler and Debugger.&nbsp; One of the features I implemented for VB 9.0 is lexical closure support.&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[78,94,117],"class_list":["post-6153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-jared-parsons","tag-linqvb9","tag-orcas"],"acf":[],"blog_post_summary":"<p>&nbsp; My name is Jared Parsons and I am a developer on the VB Compiler and Debugger.&nbsp; One of the features I implemented for VB 9.0 is lexical closure support.&nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/6153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=6153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/6153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=6153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=6153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=6153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}