{"id":12995,"date":"2015-12-03T11:47:57","date_gmt":"2015-12-03T16:47:57","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12995"},"modified":"2015-12-03T11:47:57","modified_gmt":"2015-12-03T16:47:57","slug":"variable-capture-and-loops","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/12\/03\/variable-capture-and-loops\/","title":{"rendered":"Variable Capture and Loops"},"content":{"rendered":"<p><a href=\"http:\/\/timekl.com\/\/blog\/2015\/11\/26\/variable-capture-in-go-vs-swift\/\">Tim Ekl<\/a>:<\/p>\n<blockquote cite=\"http:\/\/timekl.com\/\/blog\/2015\/11\/26\/variable-capture-in-go-vs-swift\/\">\n<p>The difference, it turns out, has to do with how variables are bound in loops,\nand how values are captured in anonymous functions. The Swift (and Objective-C)\nbehavior &#8211; which I was most used to at the time of writing &#8211; was to <strong>bind <code>i<\/code>\nas a different immutable value<\/strong> in each loop iteration, then capture a\nreference to that value each time through.<\/p>\n\n<p>Go, on the other hand, binds a <strong>single mutable value for the entire loop<\/strong>,\nthen captures a reference to that single variable instead, only getting the\nvalue in question at the time the function is executed.<\/p>\n\n<p>[&#8230;]<\/p>\n\n<p>Interestingly enough, we can even &ldquo;introduce&rdquo; this bug in Swift code by using a\nC-like loop instead of the nicer <code>for<\/code>&#8211;<code>in<\/code> syntax[&#8230;] Since this style explicitly uses a single mutable <code>i<\/code> for the entire loop,\nrather than binding a new <code>i<\/code> for each iteration, the &ldquo;buggy&rdquo; behavior &#8211;\nprinting five sixes &#8211; occurs. Swift is even kind enough to make the mutability\nof <code>i<\/code> here more explicit, by requiring it be annotated <code>var<\/code> in the loop\ndeclaration.<\/p>\n<\/blockquote>\n<p>See also: <a href=\"http:\/\/ericasadun.com\/2014\/08\/26\/swift-capturing-references-in-closures\/\">Capturing references in closures<\/a>, <a href=\"http:\/\/www.russbishop.net\/swift-capture-lists\">Capture Lists<\/a>, <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Cocoa\/Conceptual\/Blocks\/Articles\/bxVariables.html#\/\/apple_ref\/doc\/uid\/TP40007502-CH6-SW6\">The __block Storage Type<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tim Ekl: The difference, it turns out, has to do with how variables are bound in loops, and how values are captured in anonymous functions. The Swift (and Objective-C) behavior &#8211; which I was most used to at the time of writing &#8211; was to bind i as a different immutable value in each loop [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[168,131,846,46,54,71,901],"class_list":["post-12995","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-blocks","tag-bug","tag-go-language","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=12995"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12995\/revisions"}],"predecessor-version":[{"id":12996,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12995\/revisions\/12996"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}