{"id":33317,"date":"2021-08-04T16:09:27","date_gmt":"2021-08-04T20:09:27","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=33317"},"modified":"2021-08-04T16:09:27","modified_gmt":"2021-08-04T20:09:27","slug":"swift-guard-capture-specifier-pitch","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/08\/04\/swift-guard-capture-specifier-pitch\/","title":{"rendered":"Swift &ldquo;guard&rdquo; Capture Specifier Pitch"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/guard-capture-specifier-for-closure-capture-lists\/50805\">Cal Stephens<\/a> (<a href=\"https:\/\/twitter.com\/calstephens98\/status\/1421856826694328324\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/guard-capture-specifier-for-closure-capture-lists\/50805\"><p><code>guard<\/code> captures behave like <code>weak<\/code> captures (e.g. <code>guard<\/code> captures do not retain the captured value), but the closure body is only executed if the captured objects still exist.<\/p><p>[&#8230;]<\/p><p>As of <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0269-implicit-self-explicit-capture.md\">SE-0269<\/a>, strong and <code>unowned<\/code> captures of <code>self<\/code> enable implicit <code>self<\/code> calls within the body of escaping closures. This is not straightforward to support for <code>weak<\/code> closures in the general case, and was <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0269-implicit-self-explicit-capture.md#weak-captures-of-self\">intentionally excluded<\/a> from SE-0269.<\/p><p>[&#8230;]<\/p><p><code>guard let value = value else { return }<\/code> is quite a bit of boilerplate in this context.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/05\/12\/fixing-swifts-if-let-syntax\/\">Fixing Swift&rsquo;s &ldquo;if let&rdquo; Syntax<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/03\/23\/dealing-with-weak-in-closure-based-delegation\/\">Dealing With Weak in Closure-based Delegation<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/04\/05\/stop-the-weak-strong-swift-dance\/\">Stop the weak-strong Swift Dance<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Cal Stephens (tweet): guard captures behave like weak captures (e.g. guard captures do not retain the captured value), but the closure body is only executed if the captured objects still exist.[&#8230;]As of SE-0269, strong and unowned captures of self enable implicit self calls within the body of escaping closures. This is not straightforward to support [&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":"2021-08-04T20:09:32Z","apple_news_api_id":"5a718b67-2332-413f-a211-3995b57256b3","apple_news_api_modified_at":"2021-08-04T20:09:32Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AWnGLZyMyQT-iETmVtXJWsw","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":[55,46,571,71,901],"class_list":["post-33317","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-languagedesign","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33317","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=33317"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33317\/revisions"}],"predecessor-version":[{"id":33318,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/33317\/revisions\/33318"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=33317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=33317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=33317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}