{"id":16025,"date":"2016-10-11T10:10:26","date_gmt":"2016-10-11T14:10:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16025"},"modified":"2020-04-21T13:52:54","modified_gmt":"2020-04-21T17:52:54","slug":"optional-non-escaping-swift-closures","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/10\/11\/optional-non-escaping-swift-closures\/","title":{"rendered":"Optional Non-Escaping Swift Closures"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2016\/10\/optional-non-escaping-closures\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2016\/10\/optional-non-escaping-closures\/\">\n<p>However, it&rsquo;s impossible to create a reference cycle with a non-escaping closure &mdash; the compiler can guarantee that the closure will have released all objects it captured by the time the function returns. For this reason, the compiler only requires explicit references to <code>self<\/code> for <em>escaping<\/em> closures. This makes non-escaping closures significantly more pleasant to use.<\/p>\n<p>[&#8230;]<\/p>\n<p>Beginning in Swift 3, non-escaping closures <a title=\"Swift Evolution Proposal SE-0103: Make non-escaping closures the default\" href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0103-make-noescape-default.md\">are now the default<\/a>. If you want to allow a closure parameter to escape, you need to add the <code>@escaping<\/code> annotation to the type.<\/p>\n<p>[&#8230;]<\/p>\n<p>There&rsquo;s a catch to the non-escaping-by-default rule: it only applies to closures in <em>immediate function parameter position<\/em>, i.e. any function argument that has a function type. All other closures are escaping.<\/p>\n<p>[&#8230;]<\/p>\n<p>There&rsquo;s currently no way to force an optional closure to be non-escaping, but in many situations, you can probably avoid making the argument optional by providing a default value for the closure.<\/p>\n<\/blockquote>\n<p>Or you can <a href=\"https:\/\/forums.swift.org\/t\/escaping-may-only-be-applied-to-parameters-of-function-type\/4017\/9\">use overloading<\/a>.<\/p>\n\n<p>Update (2018-06-11): See also: <a href=\"https:\/\/www.jessesquires.com\/blog\/why-optional-swift-closures-are-escaping\/\">Jesse Squires<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: However, it&rsquo;s impossible to create a reference cycle with a non-escaping closure &mdash; the compiler can guarantee that the closure will have released all objects it captured by the time the function returns. For this reason, the compiler only requires explicit references to self for escaping closures. This makes non-escaping closures significantly more [&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":"2018-06-11T20:59:52Z","apple_news_api_id":"e255815a-9b8b-4468-912f-11daf801be4e","apple_news_api_modified_at":"2020-04-21T17:52:57Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A4lWBWpuLRGiRLxHa-AG-Tg","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],"class_list":["post-16025","post","type-post","status-publish","format-standard","hentry","category-programming-category","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\/16025","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=16025"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16025\/revisions"}],"predecessor-version":[{"id":28724,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16025\/revisions\/28724"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}