{"id":10297,"date":"2014-12-12T12:10:35","date_gmt":"2014-12-12T17:10:35","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10297"},"modified":"2014-12-13T16:20:49","modified_gmt":"2014-12-13T21:20:49","slug":"tearing-down-swifts-optional-pyramid-of-doom","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/12\/12\/tearing-down-swifts-optional-pyramid-of-doom\/","title":{"rendered":"Tearing Down Swift&rsquo;s Optional Pyramid of Doom"},"content":{"rendered":"<p><a href=\"http:\/\/www.scottlogic.com\/blog\/2014\/12\/08\/swift-optional-pyramids-of-doom.html\">Colin Eberhardt<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.scottlogic.com\/blog\/2014\/12\/08\/swift-optional-pyramids-of-doom.html\">\n<p>With Swift, pyramids become an issue when you have to unwrap multiple optional values before performing some logic [&#8230;] In the above code, the <code>println<\/code> statement will only be executed if all three of the optional variable <code>a<\/code>, <code>b<\/code>, and <code>c<\/code> are non nil. The more optionals your code relies on, the deeper the nesting becomes.<\/p>\n<p>[&#8230;]<\/p>\n<p>It is actually quite a straightforward task to move the nested if-let statements into a utility function, where a given function is only invoked if all the optional parameters are non nil [&#8230;] The above function unwraps the optional parameters and invokes the given function with the unwrapped results. Notice that the unwrapped variables have the same name, and shadow, the optional parameters, <a href=\"https:\/\/github.com\/raywenderlich\/swift-style-guide\/issues\/64#issuecomment-64116223\">a naming convention proposed by Sam Davies<\/a>, which I quite like.<\/p>\n<\/blockquote>\n<p>This is still less than ideal, though, because now your code is running inside of an anonymous function. You can&rsquo;t, for example, return out of the original scope. This is an example of where it would be helpful if Swift had macros, although I think this particular use case is important enough that the language should have a built-in syntax.<\/p>\n<p>Update (2014-12-13): <a href=\"http:\/\/minutestomidnight.net\/blog\/2014\/12\/improving-swift-optional-unwrapping\">Tim Schmitz<\/a>:<\/p>\n<blockquote cite=\"http:\/\/minutestomidnight.net\/blog\/2014\/12\/improving-swift-optional-unwrapping\"><p>I think it's worth making some tweaks to how optional unwrapping works, and I have two suggestions.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Colin Eberhardt: With Swift, pyramids become an issue when you have to unwrap multiple optional values before performing some logic [&#8230;] In the above code, the println statement will only be executed if all three of the optional variable a, b, and c are non nil. The more optionals your code relies on, the deeper [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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,941,71,901],"class_list":["post-10297","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-macros","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10297","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=10297"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10297\/revisions"}],"predecessor-version":[{"id":10309,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10297\/revisions\/10309"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}