{"id":4301,"date":"2009-03-27T18:49:00","date_gmt":"2009-03-27T18:49:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/27\/get-closure-with-getnewclosure\/"},"modified":"2019-02-18T13:12:45","modified_gmt":"2019-02-18T20:12:45","slug":"get-closure-with-getnewclosure","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/get-closure-with-getnewclosure\/","title":{"rendered":"Get closure with GetNewClosure"},"content":{"rendered":"<p>Have you ever created scriptblocks on the fly, say in a foreach loop, and they totally mess up because they all have the same value?<\/p>\n<p>This is something sort of advanced, and typically used when you\u2019re proxying an object.<\/p>\n<p>The most basic example would be, taken from (<a href=\"http:\/\/www.powershellcommunity.org\/Forums\/tabid\/54\/aff\/1\/aft\/2506\/afv\/topic\/Default.aspx\">http:\/\/www.powershellcommunity.org\/Forums\/tabid\/54\/aff\/1\/aft\/2506\/afv\/topic\/Default.aspx<\/a>)<\/p>\n<p>function add([int]$x) { return { param([int]$y) return $y + $x } }<br \/>$m2 = add 2<br \/>$m5 = add 5<br \/>&amp;$m2 3 #expected:&nbsp; 5 actual: 3<br \/>&amp;$m5 6 #expected: 11 actual: 6<\/p>\n<p>The value for $x is $null for both.<\/p>\n<p>Why is that? Its because $x is $null in the local scope, the script block doesn\u2019t remember the $x.<\/p>\n<p>If we do<\/p>\n<p>$x = 1000<br \/>&amp;$m2 3 #expected:&nbsp; 5 actual: 1003<br \/>&amp;$m5 6 #expected: 11 actual: 1006<\/p>\n<p>This can be fixed in V2 using closures.<\/p>\n<p>function add([int]$x) { return { param([int]$y) return $y + $x }.GetNewClosure() }<br \/>$m2 = add 2<br \/>$m5 = add 5<br \/>&amp;$m2 3 #expected:&nbsp; 5 actual: 5<br \/>&amp;$m5 6 #expected: 11 actual: 11<\/p>\n<p>Closures are also great for ScriptProperties and ScriptMethods.<\/p>\n<p>Hope this helps,<br \/>Ibrahim<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever created scriptblocks on the fly, say in a foreach loop, and they totally mess up because they all have the same value? This is something sort of advanced, and typically used when you\u2019re proxying an object. The most basic example would be, taken from (http:\/\/www.powershellcommunity.org\/Forums\/tabid\/54\/aff\/1\/aft\/2506\/afv\/topic\/Default.aspx) function add([int]$x) { return { param([int]$y) return [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[97,137,54],"class_list":["post-4301","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-advanced-functions","tag-ctp3","tag-scriptblock"],"acf":[],"blog_post_summary":"<p>Have you ever created scriptblocks on the fly, say in a foreach loop, and they totally mess up because they all have the same value? This is something sort of advanced, and typically used when you\u2019re proxying an object. The most basic example would be, taken from (http:\/\/www.powershellcommunity.org\/Forums\/tabid\/54\/aff\/1\/aft\/2506\/afv\/topic\/Default.aspx) function add([int]$x) { return { param([int]$y) return [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4301"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4301\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}