{"id":32460,"date":"2021-05-12T17:07:56","date_gmt":"2021-05-12T21:07:56","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32460"},"modified":"2021-06-18T15:52:12","modified_gmt":"2021-06-18T19:52:12","slug":"fixing-swifts-if-let-syntax","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/05\/12\/fixing-swifts-if-let-syntax\/","title":{"rendered":"Fixing Swift&rsquo;s &ldquo;if let&rdquo; Syntax"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/lets-fix-if-let-syntax\/48188\">Craig Hockenberry<\/a> (<a href=\"https:\/\/twitter.com\/chockenberry\/status\/1392170675884859395\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/lets-fix-if-let-syntax\/48188\"><p>The if-let syntax discourages long variable names because there&rsquo;s no autocomplete for optional variables that are in scope. It&rsquo;s not uncommon to see things like this because programmers are notoriously lazy:<\/p><pre>if let favc = fooAutomationViewController { &#8230; }<\/pre><p>Any code within that block that references favc won&rsquo;t be very readable.<\/p><p>This maintenance issue is exacerbated by name refactoring. Say you want to change fooAutomationViewController to a barAutomatedViewController. The refactored code will then become:<\/p><pre>if let favc = barAutomatedViewController { &#8230; }<\/pre><\/blockquote>\n<p>The <code>if let fooAutomationViewController = fooAutomationViewController<\/code> repetition is one of the more annoying things about writing and reading Swift. Allowing this to be written as just <code>if let fooAutomationViewController<\/code> seems like the obvious solution. For those arguing that this is confusing or hard to learn, I don&rsquo;t see how it&rsquo;s any less clear than what it would be replacing. That said, I wouldn&rsquo;t be opposed to something like <a href=\"https:\/\/gist.github.com\/erica\/aea6a1c55e9e92f843f92e2b16879b0f\">unwrap<\/a>.<\/p>\n\n<p id=\"fixing-swifts-if-let-syntax-update-2021-06-18\">Update (2021-06-18): <a href=\"https:\/\/blog.metaobject.com\/2021\/06\/if-let-it-be.html\">Marcel Weiher<\/a>:<\/p>\n<blockquote cite=\"https:\/\/blog.metaobject.com\/2021\/06\/if-let-it-be.html\"><p>When I added <code>ifNotNil:<\/code> some time ago, I used the same logic, but it turns out the object\nis now actually potentially interesting.  So <code>ifNotNil:<\/code> now passes the now-known-to-be-non-nil\nvalue to the block and can be used as follows[&#8230;]<\/p>\n<p>[&#8230;]<\/p><p>This doesn&rsquo;t eliminate the duplication, but does avoid the issue of having the newly introduced variable name precede the original variable.  Well, that and the whole\nweird <code>if let<\/code> in the first place.<\/p><\/blockquote>\n<p>You can do this in Swift by calling <code>map(_:)<\/code> on the optional. It&rsquo;s useful in some cases, but, since it relies on a closure like <code>forEach(_:)<\/code>, it&rsquo;s often not a good fit because it doesn&rsquo;t compose well with other language features (additional optionals to be unwrapped, <code>continue<\/code>, <code>return<\/code>, etc.).<\/p>","protected":false},"excerpt":{"rendered":"<p>Craig Hockenberry (tweet): The if-let syntax discourages long variable names because there&rsquo;s no autocomplete for optional variables that are in scope. It&rsquo;s not uncommon to see things like this because programmers are notoriously lazy:if let favc = fooAutomationViewController { &#8230; }Any code within that block that references favc won&rsquo;t be very readable.This maintenance issue is [&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-05-12T21:07:59Z","apple_news_api_id":"04c21a01-44b5-4daf-bf53-f642d39bf230","apple_news_api_modified_at":"2021-06-18T19:52:16Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/ABMIaAUS1Ta-_U_ZC05vyMA","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,71,901,226],"class_list":["post-32460","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language","tag-xcode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32460","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=32460"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32460\/revisions"}],"predecessor-version":[{"id":32898,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32460\/revisions\/32898"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}