{"id":29479,"date":"2020-07-13T16:30:03","date_gmt":"2020-07-13T20:30:03","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=29479"},"modified":"2020-07-30T16:28:55","modified_gmt":"2020-07-30T20:28:55","slug":"implicit-capturing-of-self-in-swift-5-3","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/07\/13\/implicit-capturing-of-self-in-swift-5-3\/","title":{"rendered":"Implicit Capturing of Self in Swift 5.3"},"content":{"rendered":"<p><a href=\"https:\/\/swiftbysundell.com\/tips\/implicit-capturing-of-self\/\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/swiftbysundell.com\/tips\/implicit-capturing-of-self\/\">\n<p>In Swift 5.3, however, using <code>self<\/code> in the above kind of situation is no longer required, and we can simply access properties and methods belonging to value types (such as SwiftUI views, or any other type of struct) without any additional prefixes &mdash; regardless if that&rsquo;s done within an escaping closure or not[&#8230;]<\/p>\n<p>That&rsquo;s really neat, and further makes <a href=\"https:\/\/swiftbysundell.com\/articles\/the-swift-51-features-that-power-swiftuis-api\">SwiftUI&rsquo;s DSL<\/a> feel even more lightweight. However, it&rsquo;s important to remember that the same kind of capturing still occurs &mdash; meaning that the above <code>ListView<\/code> value (and the <code>ListViewModel<\/code> instance that it contains) will still be captured by our closures. Within the context of SwiftUI, though, that&rsquo;s not likely to become a problem &mdash; since SwiftUI views are just lightweight structs that don&rsquo;t hold any strong references to their subviews.<\/p>\n<\/blockquote>\n\n<p>This is perhaps <a href=\"https:\/\/twitter.com\/krzyzanowskim\/status\/1282057063888310274\">confusing<\/a>, but it doesn&rsquo;t seem like it will cause unwanted captures because it&rsquo;s limited to value types.<\/p>\n\n<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0269-implicit-self-explicit-capture.md#bound-method-references\">SE-0269<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0269-implicit-self-explicit-capture.md#bound-method-references\">\n<p>Whenever <code>self<\/code> is declared explicitly in an escaping closure&rsquo;s capture list, or its type is a value type, any code inside that closure can use names which resolve to members of the enclosing type, without specifying <code>self.<\/code> explicitly.<\/p>\n<p>[&#8230;]<\/p>\n<p>While this proposal opens up implicit <code>self<\/code> in situations where we can be reasonably sure that we will not cause a reference cycle, there are other cases where implicit <code>self<\/code> is currently allowed that we may want to disallow in the future. One of these is allowing bound method references to be passed into escaping contexts without making it clear that such a reference captures <code>self<\/code>.<\/p><\/blockquote>\n\n<p>I still think closures are an area of Swift that needs work. I wish it were harder to accidentally strongly capture <code>self<\/code> in reference types and less verbose to use it weakly.<\/p>\n\n<p>Previously:<\/p>\n<ul>\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>\n\n<p id=\"implicit-capturing-of-self-in-swift-5-3-update-2020-07-30\">Update (2020-07-30): <a href=\"https:\/\/twitter.com\/_sjs\/status\/1282903700894183425\">Sami Samhuri<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/_sjs\/status\/1282903700894183425\"><p>It would be nice to concisely say that a closure should be discarded when a weakly-captured param goes away. I <a href=\"https:\/\/gist.github.com\/samsonjs\/c398f903b0d5fa66b15ce3ba323f0f83\">imagined<\/a> what that could look like and now I want @ClosureWrapper.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>John Sundell: In Swift 5.3, however, using self in the above kind of situation is no longer required, and we can simply access properties and methods belonging to value types (such as SwiftUI views, or any other type of struct) without any additional prefixes &mdash; regardless if that&rsquo;s done within an escaping closure or not[&#8230;] [&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":"2020-07-13T20:30:07Z","apple_news_api_id":"f42edafb-c626-4b36-a877-d7d44adc53f7","apple_news_api_modified_at":"2020-07-30T20:29:00Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/A9C7a-8YmSzaod9fUStxT9w","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":[46,571,71,901,1812],"class_list":["post-29479","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-memory-management","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29479","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=29479"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29479\/revisions"}],"predecessor-version":[{"id":29644,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/29479\/revisions\/29644"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=29479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=29479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=29479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}