{"id":20934,"date":"2018-03-23T15:20:11","date_gmt":"2018-03-23T19:20:11","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20934"},"modified":"2025-10-30T09:23:15","modified_gmt":"2025-10-30T13:23:15","slug":"dealing-with-weak-in-closure-based-delegation","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/03\/23\/dealing-with-weak-in-closure-based-delegation\/","title":{"rendered":"Dealing With Weak in Closure-based Delegation"},"content":{"rendered":"<p><a href=\"https:\/\/medium.com\/anysuggestion\/preventing-memory-leaks-with-swift-compile-time-safety-49b845df4dc6\">Oleg Dreyman<\/a> (via <a href=\"http:\/\/www.figure.ink\/blog\/2018\/3\/18\/dealing-with-weak-in-closure-based-delegation\">Joshua Emmons<\/a>):<\/p>\n<blockquote cite=\"https:\/\/medium.com\/anysuggestion\/preventing-memory-leaks-with-swift-compile-time-safety-49b845df4dc6\"><p>Let&rsquo;s look at the core of the problem: 99% of the time, when assigning a delegation callback, there should be a <code>[weak self]<\/code> capture list, but <em>nothing <\/em>is actually preventing ourselves from omitting it. No errors, no warnings, nothing. What if instead we could force the correct behavior?<\/p>\n<p>[&#8230;]<\/p>\n<p>Leveraging the power of Swift generics, we can do better:<\/p>\n<pre>struct DelegatedCall&lt;Input&gt; {\n\n    private(set) var callback: ((Input) -&gt; Void)?\n    \n    mutating func delegate&lt;Object : AnyObject&gt;(to object: Object, with callback: @escaping (Object, Input) -&gt; Void) {\n        self.callback = { [weak object] input in\n            guard let object = object else {\n                return\n            }\n            callback(object, input)\n        }\n    }\n}<\/pre>\n<\/blockquote>\n\n<p id=\"dealing-with-weak-in-closure-based-delegation-update-2025-10-30\">Update (<a href=\"#dealing-with-weak-in-closure-based-delegation-update-2025-10-30\">2025-10-30<\/a>): <a href=\"https:\/\/mas.to\/@nickmain\/114479995210607181\">Nick Main<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mas.to\/@nickmain\/114479995210607181\"><p>I&rsquo;ve used keypaths in our codebase to help prevent problems with forgetting to use <code>[weak self]<\/code> in closures.<\/p><p>Instead of a closure we take an object+keypath pair and the object is held weakly. Accessing a computed property via keypath is like a zero-arg method in these use cases.<\/p><p>Full method keypaths will make this pattern capable of replacing many more closure use cases for us.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Oleg Dreyman (via Joshua Emmons): Let&rsquo;s look at the core of the problem: 99% of the time, when assigning a delegation callback, there should be a [weak self] capture list, but nothing is actually preventing ourselves from omitting it. No errors, no warnings, nothing. What if instead we could force the correct behavior? [&#8230;] Leveraging [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-07-13T20:25:45Z","apple_news_api_id":"dd035bf4-25a3-4216-925d-86cc8c5ef03f","apple_news_api_modified_at":"2025-10-30T13:23:17Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A3QNb9CWjQhaSXYbMjF7wPw","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,27,46,571,71,901],"class_list":["post-20934","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-craft","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\/20934","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=20934"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20934\/revisions"}],"predecessor-version":[{"id":49840,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20934\/revisions\/49840"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}